Skip to content

Instantly share code, notes, and snippets.

@elliotf
Last active December 13, 2015 21:48
Show Gist options
  • Save elliotf/4979332 to your computer and use it in GitHub Desktop.
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".
#!/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