Last active
December 13, 2015 21:48
-
-
Save elliotf/4979332 to your computer and use it in GitHub Desktop.
Mongoose error due to incorrect schema statement: MissingSchemaError: Schema hasn't been registered for model "undefined".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema | |
; | |
var catSchema = new Schema({ | |
name: String | |
}); | |
var Cat = mongoose.model('Cat', catSchema); | |
var personSchema = Schema({ | |
name: String | |
, cats: {type: [Schema.Types.ObjectId], ref: 'Cat'} | |
// ^ ^ | |
// should be: | |
//, cats: [{type: Schema.Types.ObjectId, ref: 'Cat'}] | |
// ^ ^ | |
}); | |
var Person = mongoose.model('Person', personSchema); | |
mongoose.connect('mongodb://localhost/mongoose-schema-facepalm'); | |
Cat.create({name: 'fluffy'}, function(err, cat){ | |
Person.create({name: 'bob', cats: [cat]}, function(err, person){ | |
Person.findById(person.id).populate('cats').exec(function(err, person){ | |
console.log(person.cats[0].name); | |
}); | |
}); | |
}); | |
// results in: | |
// /home/efoster/work/facepalm/node_modules/mongoose/lib/utils.js:397 | |
// throw err; | |
// ^ | |
// MissingSchemaError: Schema hasn't been registered for model "undefined". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment