title: Why mongoose.model.create's Callback Isn't Executed from within Express 3.x HTTP Request Handler (connect-mongodb is the cause, actually)
body: <p>To put the problem stright in code:
  </p>
  
  <pre><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.');
  }
  </code></pre>
  
  <p>Express is 3.x, Mongoose is 3.x.<br>
  Imprtant details of initialization:
  </p>
  
  <pre><code>mongoose.connect(mongourl);
  var mongoStore = require('connect-mongodb'); // Not 'connect-mongo', sic!
  var store = new mongoStore({db: mongoose.connection.db}, resultHandler);
  </code></pre>
  
  <p>You can see the whole code at <a href="https://github.com/ipetrovilya/discussions_with_ipetrovilya/blob/master/ExpressMongooseCreateModelHangs/server.js" rel="nofollow">GitHub</a>.<br>
  Here is a minified version of main script from there:
  </p>
  
  <pre><code>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: 'some.email@example.com',
    }
  
    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);
  </code></pre>