简体   繁体   中英

mongoose “Schema hasn't been registered” error when trying to populate

This is the error that is plaguing me:

err:  { MongooseError: Schema hasn't been registered for model "function model(doc, fields, skipId) {
  if (!(this instanceof model)) {
    return new model(doc, fields, skipId);
  }
  Model.call(this, doc, fields, skipId);
}".
Use mongoose.model(name, schema)
at NativeConnection.Connection.model (/Users/riordan/prog/tuts/reddice/server/node_modules/mongoose/lib/connection.js:968:11)
at getModelsMapForPopulate (/Users/riordan/prog/tuts/reddice/server/node_modules/mongoose/lib/model.js:3472:20)
at populate (/Users/riordan/prog/tuts/reddice/server/node_modules/mongoose/lib/model.js:3026:15)
at _populate (/Users/riordan/prog/tuts/reddice/server/node_modules/mongoose/lib/model.js:2994:5)
at Function.Model.populate (/Users/riordan/prog/tuts/reddice/server/node_modules/mongoose/lib/model.js:2954:5)
at Immediate.<anonymous> (/Users/riordan/prog/tuts/reddice/server/node_modules/mongoose/lib/query.js:1377:17)
at Immediate.<anonymous> (/Users/riordan/prog/tuts/reddice/server/node_modules/mquery/lib/utils.js:137:16)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)

This line only creates the above error when I use populate as shown:

Project.findOne({ name: projectName }).populate({path:'inputs'}).exec(function (err, doc) {

So this line works fine (it just doesn't populate obviously haha):

Project.findOne({ name: projectName }).exec(function (err, doc) {


I have read a few stackoverflow questions/answers that are related to my own but could not find a fix.

I have checked that my mongoose connection holds both models prior to using "populate" via console.log(mongoose.connection) and I have checked for typos.

So yeah I'd really appreciate some help :)

The answer was actually in where I referenced the model. I had it referencing a model variable when you are supposed to reference the name of the model in a string eg

field: {type: Schema.Types.ObjectId, ref 'ModelName'}

and i had

field: {type: Schema.Types.ObjectId, ref Model}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM