just ran npm update, now I get this error: #1249

chovy opened this Issue Dec 13, 2012 · 17 comments


None yet

5 participants

chovy commented Dec 13, 2012
Caught exception: { message: 'Schema hasn\'t been registered for model "User".\nUse mongoose.model(name, schema)',
  name: 'MissingSchemaError' } MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
    at NativeConnection.Connection.model (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/connection.js:573:11)
    at model.populate [as _populate] (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/model.js:142:9)
    at next (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/model.js:269:14)
    at init (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/model.js:215:12)
    at model.init (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/model.js:202:3)
    at cb (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/query.js:1485:14)
    at exports.tick (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/utils.js:429:16)
    at Cursor.toArray (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:159:9)
    at Cursor.each (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:199:11)
    at Cursor.nextObject (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:551:35)

I'm not even sure where my code is broken. It worked fine the other day. The only clue I can give is I cleared out my users collection and all other collections, so its a fresh database.


please read the release notes for 3.5.0


  • fixed; throw error when user attempts to overwrite a model

Previously, if we attempted to overwrite a model by passing the same model name with a new schema to mongoose.model or connection.model, the previously compiled model would be returned, and the programming error would be swallowed. Attempting to overwrite a previously compiled model is now properly treated as an error.

@aheckmann aheckmann closed this Dec 13, 2012
chovy commented Dec 13, 2012

So what do I need to do differently?


do not attempt to overwrite your models. just do it once when you start your app.

mongoose.model('something', yourSchema)

chovy commented Dec 13, 2012

Ok, this must be the problem. I require('../models/user'); all over the place in my code, basically whenever it is needed...what's the best practice for loading models?

Actually, the error is claiming the schema is not defined, and you're saying its being redefined.

This is what I have at the bottom of my schema:

module.exports = db.model('User', UserSchema);

ah, sorry i missed that.

models declared on connections (db.model('User', UserSchema)) are now only cached on the connection itself, not cached globally on mongoose. this means that calling mongoose.model('User') will not return the compiled model from your connection. this change was the fix for #1209.

to gain access to the model you'll need to either

  1. call db.model('User') on the same connection used to compile it OR
  2. add the model globally to mongoose with mongoose.model('User', userSchema)
chovy commented Dec 14, 2012

I changed:

module.exports = db.model('User', UserSchema);


module.exports = mongoose.model('User', UserSchema);

...but still doesn't work. my app just hangs.

What's a better way to use models and share a db connection since the above no longer works, here is my ./model/user.js

var mongoose = require('mongoose'), cfg = require('../config')
, Schema = mongoose.Schema
, db = mongoose.createConnection(cfg.mongo.uri, cfg.mongo.db);

module.exports = db.model('Item', ItemSchema);

Many folks keep it simple and go with mongoose.connect() when their app starts up and then use mongoose.model() everywhere. This opens the default mongoose connection that mongoose.model uses for all models. It "just works".

additional info

models are tied to a connection. if the connection they are associated with hasn't been opened then you'll see that "hanging" behavior while it waits for the connection to open.

typically only one connection is needed in an application. just opening it and reusing it throughout the application is best. just close it when your app shuts down (mongoose.disconnect())

some scenarios dictate the use of more than one connection, which is where mongoose.createConnection comes in.

each "connection" is backed by a connectionPool in the driver.

chovy commented Dec 14, 2012

Can you put an example of how to properly use mongoose on the mongoosejs homepage? That example live now seems to not be preferred method. I'm sitll new to mongoose/node so I apologize -- I tried doing this but it does not work:

module.exports = mongoose.model('User', UserSchema);


yeah you're not the only one who's mentioned that example. i'll update it now.

at this point its hard to say what isn't working.
heres an example of what i mean (start in init.js): https://gist.github.com/4288767

chovy commented Dec 14, 2012

Here is my code, but this am not sure what i'm doing wrong (still hangs):




you've opened an individual connection on line 4 but compile your model against the default connection on line 18 using mongoose.model. models are associated to a connection which must be opened. either use mongoose.connect() on line 4 or db.model() on line 18

chovy commented Dec 15, 2012

Sorry for the hassle -- this works fine now in app if I use mongoose.connect in my app.js, however now my mocha tests are all failing:


$ make test
NODE_ENV: test

        throw arguments[1]; // Unhandled 'error' event
Error: Trying to open unclosed connection.
    at NativeConnection.Connection.open (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/connection.js:203:15)
    at Mongoose.connect (/home/ettinger/projects/wishd.me/wishd/node_modules/mongoose/lib/index.js:154:15)
    at Object.<anonymous> (/home/ettinger/projects/wishd.me/wishd/test/model.user.js:3:17)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Mocha.loadFiles (/home/ettinger/projects/wishd.me/wishd/node_modules/mocha/lib/mocha.js:137:27)
    at Array.forEach (native)
    at Mocha.loadFiles (/home/ettinger/projects/wishd.me/wishd/node_modules/mocha/lib/mocha.js:134:14)
    at Mocha.run (/home/ettinger/projects/wishd.me/wishd/node_modules/mocha/lib/mocha.js:290:31)
    at Object.<anonymous> (/home/ettinger/projects/wishd.me/wishd/node_modules/mocha/bin/_mocha:329:7)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
make: *** [test] Error 1


not sure about why mocha is failing. maybe mongoose.connect(cfg.mongo.uri, cfg.mongo.db) is in every file which would be reopening the same connection over and over?

chovy commented Dec 17, 2012

I had to add this line to catch it so it would not try to re-open an existing connection:

mongoose.connection.on('error', function(err){});
pvencill commented Jan 3, 2013

so the error, "Schema hasn't been registered for model" is thrown when a schema has previously been registered? That's somewhat counter-intuitive.


@chovy your hack works. Tx. There shall be a better solution though.


Appreciate if you can tell me what I'm doing wrong:

var HierarchySchema = require('../models/HierarchyModel').HierarchySchema;
var db = mongoose.connection;
db.once('open', fillData);
Hierarchy = db.model('Hierarchy', HierarchySchema);
console.info(Hierarchy.modelName); // logs 'Hierarchy'
Hierarchy.remove({}, cb); // everything's fine at this moment

var _he = new Hierarchy({
level : hierarchy.level

_he.save(function(err, saved) {...}); // data is saved, I can see it in the database

console.info('Known models : ' + util.inspect(db.modelNames())); // logs [ 'Hierarchy' ]
Hierarchy.find({}, function(err, results) { ...}

At the latest point, I get the following error:

throw new MongooseError.MissingSchemaError(name);
MissingSchemaError: Schema hasn't been registered for model "undefined".
Use mongoose.model(name, schema)
at NativeConnection.Connection.model (/Users/alexs/WebstormProjects/Project/node_modules/mongoose/lib/connection.js:625:11)
at model.Model.init (/Users/alexs/WebstormProjects/Project/node_modules/mongoose-schema-extend/index.js:61:25)
at completeMany (/Users/alexs/WebstormProjects/Project/node_modules/mongoose/lib/query.js:1042:12)
at Object.cb (/Users/alexs/WebstormProjects/Project/node_modules/mongoose/lib/query.js:1009:11)
at Object._onImmediate (/Users/alexs/WebstormProjects/Project/node_modules/mongoose/node_modules/mquery/lib/utils.js:126:16)
at processImmediate as _immediateCallback

The callback function of the find method is not called.

Please let me know if you need more code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment