Mongoose: "mongoose.connection.close()" not work

Created on 25 Apr 2011  路  1Comment  路  Source: Automattic/mongoose

I will get the following exception:

============================================ checkMaster_::0::undefined

node.js:178
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Cannot call method 'forEach' of undefined
    at [object Object].checkMaster_ (/home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/db.js:611:31)
    at [object Object].executeCommand (/home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/db.js:555:12)
    at /home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/db.js:485:42
    at /home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/db.js:518:7
    at /home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/cursor.js:167:11
    at /home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/cursor.js:467:28
    at [object Object].close (/home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/cursor.js:625:17)
    at [object Object].nextObject (/home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/cursor.js:467:10)
    at Array.<anonymous> (/home/shawny/node/lib/node/.npm/mongoose/1.3.0/package/support/node-mongodb-native/lib/mongodb/cursor.js:161:12)
    at EventEmitter._tickCallback (node.js:170:26)

The test case:

var mongoose = require('mongoose')
     Schema = mongoose.Schema

mongoose.connect('mongodb://localhost/test');

var TestSchema = new Schema({
   user_id        : {type : Number, index : true}
  ,username       : {type : String}
});

var model_name = coll_name = 'taobao';
mongoose.model(model_name, TestSchema, coll_name);

var TAOBAO  = mongoose.model(model_name, coll_name);
var taobao  = new TAOBAO();
taobao.user_id  = 1;
taobao.username = 'shawny';
taobao.save(function(err) {
  if (err) {
     console.log('save failed');
  }
  mongoose.connection.close();
});

Most helpful comment

This is a timing issue because the code snippet is so short -- in practice, this will never happen.

To fix this in your snippet, just replace mongoose.connection.close() with:

setTimeout( function () {
  mongoose.disconnect();
}, 1000);

--Brian

>All comments

This is a timing issue because the code snippet is so short -- in practice, this will never happen.

To fix this in your snippet, just replace mongoose.connection.close() with:

setTimeout( function () {
  mongoose.disconnect();
}, 1000);

--Brian

Was this page helpful?
0 / 5 - 0 ratings