Coder Social home page Coder Social logo

Comments (3)

vkarpov15 avatar vkarpov15 commented on May 26, 2024

@naeemshaikh27 what does your schema validation failed error look like?

from mongoose-autopopulate.

naeemshaikh27 avatar naeemshaikh27 commented on May 26, 2024

its a cast error for the autopopulated field, I got the result autopopulated and was trying to save after updating the field as just objectId.

Example:
got result as,

company:{
   _id: 'some Object Id'
   name : 'some name',
   admin:'admin name'
  }
});

and now I am trying to save it as:
company: someObjectId';

Now as the schema says, company is type objectId

var schema = new mongoose.Schema({
  company:{
   type: Schema.Types.ObjectId,
   ref: 'companyProfile',
   autopopulate: { select: 'name _id  admin'},
  }
});

it should save, but i get following error-


{ [ValidationError: companyProfile validation failed]
  message: 'companyProfile validation failed',
  name: 'ValidationError',
  errors: 
   { 'company': 
      { [CastError: Cast to ObjectID failed for value "ObjectID,WNËvgÝÛ\o�¦" at path "company"]
        message: 'Cast to ObjectID failed for value "ObjectID,WNË\u0018vgÝÛ\\o�¦" at path "company"',
        name: 'CastError',
        kind: 'ObjectID',
        value: [Object],
        path: 'company',
        reason: [Object] } } }

from mongoose-autopopulate.

lykmapipo avatar lykmapipo commented on May 26, 2024

@naeemshaikh27

Try to use mongoose-autoset which will set id from instance which have already been populated.

For your model update property/field definition to

var schema = new mongoose.Schema({
  company:{
   type: Schema.Types.ObjectId,
   ref: 'companyProfile',
   autoset:true,
   autopopulate: { select: 'name admin'},
  }
});

schema.plugin(require('mongoose-autoset'));

After that you dont have to worry about set id each time you have and ref that is already populated.

Hope it helps.

from mongoose-autopopulate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.