ilyaigpetrov
ilyaigpetrov

Reputation: 3883

Why mongoose.model.create's Callback Isn't Executed from within Express 3.x HTTP Request Handler (connect-mongodb is the cause, actually)

To put the problem stright in code:

expressApp.get('/', function requestHandler(req, res) {
  userMongooseModel.create(userPropertiesDict, NEVER_CALLED_CALLBACK);
  console.log('This is printed just fine but browser awaits response forever.');
}

Express is 3.x, Mongoose is 3.x.
Imprtant details of initialization:

mongoose.connect(mongourl);
var mongoStore = require('connect-mongodb'); // Not 'connect-mongo', sic!
var store = new mongoStore({db: mongoose.connection.db}, resultHandler);

You can see the whole code at GitHub.
Here is a minified version of main script from there:

var express = require('express'),
    mongoose = require('mongoose')
    mongoStore = require('connect-mongodb');

var app = express();
var mongourl = 'mongodb://localhost/problem_users';
var port = 9001;

console.log('Connecting to MongoDB...');
mongoose.connect(mongourl);

Schema = mongoose.Schema;

attributes = {
  email: {
    type: String,
    required: true,
    unique: true
  }
};

schema = new Schema(attributes, {
  strict: true
});

var User = mongoose.model('User', schema);

app.configure(function() {

  app.use(express.cookieParser());

  app.use(express.session({
    secret: 'What bad can happen if I share this secret?',
    maxAge: new Date(Date.now() + 60*60*1000),
    store: new mongoStore({
      db: mongoose.connection.db
    }, function(err) {
      return console.log(err || 'connect-mongodb setup ok');
    })
  }));

  return app;
});

app.get('/', function(req, res) {
  console.log('Ready to register a new user...');

  var userPropertiesDict = {
    email: '[email protected]',
  }

  console.log('Calling User.create...');
  User.create(userPropertiesDict, function(err, doc) {

    console.log('WHY IS THIS MESSAGE NOT PRINTED AT ALL?');
    var done = process.stdout.write('Are we stuck at flushing?');
    if (!done) { // Is stdout blocked?
      process.stdout.on('drain', _)
    }

  });
  console.log('RESPONSE IS OVER. THIS MESSAGE IS PRINTED AT THE END AND WE HANG AFTERWARDS.');
});

app.listen(port);
console.log("Listening on port ", port);
console.log("MongoDB url ", mongourl);
console.log('Navigate your browser to http://localhost:%d', port);

Upvotes: 1

Views: 1031

Answers (1)

ilyaigpetrov
ilyaigpetrov

Reputation: 3883

Note that this problem includes connect-mongodb, not connect-mongo. Switching from connect-mongodb to connect-mongo resolves the problem in a more elegant way, so you should try it first. The following is related to connect-mongodb only.

I have figured out that problem doesn't involve express at all. Actually it deals with the order in which mongoose connection and connect-mongodb session are initialized.

When connect-mongodb session is initialized, database must be already connected. As mongoose.connect is executed asynchronously you can easy run into a problem when the session is initialized bofore connection is finished (as I did).

The sad side is that for some reason connect-mongodb doesn't throw or pass an error.

You can see the right and wrong ways of mongoose and connect-mongodb initializion in the code below.

var mongoose = require('mongoose')
    mongoStore = require('connect-mongodb');

var mongourl = 'mongodb://localhost/problem_users';

console.log("MongoDB: url ", mongourl);
console.log('MongoDB: connecting...');

/*
// THE WRONG WAY OF USING mongoose WITH connect-mongodb.
// The source below was the source of the problem.

mongoose.connect(mongourl, function(err) {
  console.log('THIS IS NOT PRINTED.');
  if (err) {
    console.log('THIS IS NOT PRINTED EITHER.');
    throw err;
  }
});

new mongoStore({
  db: mongoose.connection.db
}, function(err) {
  // The line below usually prints 2.
  console.log('MongoDB: readyState %d (2 = connecting, 1 = connected)', mongoose.connection.readyState);
  return console.log(err || 'connect-mongodb setup ok');
})
*/

// THE RIGHT WAY OF USING mongoose WITH connect-mongodb.
mongoose.connect(mongourl, {auto_reconnect: true, native_parser: true}, function(err) {
  new mongoStore({
    db: mongoose.connection.db
  }, function(err) {
    // The line below usuall prints 1.
    console.log('MongoDB: readyState %d (2 = connecting, 1 = connected)', mongoose.connection.readyState);
    return console.log(err || 'connect-mongodb setup ok');
  });
});

console.log('MongoDB: connected.')

Schema = mongoose.Schema;

attributes = {
  email: {
    type: String,
    required: true,
    unique: true
  }
};

var schema = new Schema(attributes);

var User = mongoose.model('User', schema);

function createUser(callback) {

  var userPropertiesDict = {
    email: '[email protected]',
  }

  console.log('Calling User.create...');
  User.create(userPropertiesDict, callback);
}

function NEVER_CALLED_CALLBACK(err, doc) {

    console.log('WHY IS THIS MESSAGE NOT PRINTED AT ALL?');
    var done = process.stdout.write('Are we stuck at flushing?');
    if (!done) { // Is stdout blocked?
      process.stdout.on('drain', _)
    }
}

createUser(NEVER_CALLED_CALLBACK); // This either creates new User or logs an error that it already exists.

Also see this article that helped me to settle things the right way.

Upvotes: 1

Related Questions