Mongoose.js instance.save() callback not firing
mongodb, mongoose
Solution
this is a case where you are adding the model to the global mongoose object but opening a separate connection `mongo.createConnection()` that the models are not part of. Since the model has no connection it cannot save to the db.
this is solved either by connecting to mongo on the global mongoose connection:
var connection = mongo.createConnection('mongodb://127.0.0.1/test');
// becomes
var connection = mongo.connect('mongodb://127.0.0.1/test');
or by adding your models to your separate connection:
var BookModel = mongo.model('abook', BookSchema);
// becomes
var BookModel = connection.model('abook', BookSchema);
Problem
``` var mongo = require('mongoose'); var connection = mongo.createConnection('mongodb://127.0.0.1/test'); connection.on("error", function(errorObject){ console.log(errorObject); console.log('ONERROR'); }); var Schema = mongo.Schema; var BookSchema = new Schema({ title : {type : String, index : {unique : true}}}); var BookModel = mongo.model('abook', BookSchema); var b = new BookModel({title : 'aaaaaa'}); b.save( function(e){ if(e){ console.log('error') }else{ console.log('no error') }}); ``` Neither the 'error', or 'no error' are printed to the terminal. What's more the connection.on 'error' doesn't seem to fire either. I have confirmed that MongoDb is running.