title: Query Callback Not Called When Mongoose Connection Created In Another Function
body: <p>When I query right after creating and opening mongoose connection as shown below, the query callback is hit and docs are loaded.</p>
  
  <pre class="lang-javascript prettyprint-override"><code>var db,
      mongoose = require('mongoose');
  
  ...
  
  MyClass.prototype.query = function(model, criteria, callback) {
    var options = {
      server: {
        auto_reconnect: true,
        socketOptions : {
          keepAlive: 1
        }
      }
    };
  
    mongoose.connect('mongodb://localhost/mydatabase', options);
  
    db = mongoose.connection;
  
    db.on('error', console.error.bind(console, 'Error:'));
  
    db.once('open', function () {
      model.find(criteria).exec(function(err, docs) {
        callback(err, {}, docs);
      });
    });
  };
  </code></pre>
  
  <p>However, when I create the connection in an <code>initDB</code> function and make the query later on as shown below, the callback is not called.  <code>initDB</code> is being called before <code>express</code> server is started.</p>
  
  <pre class="lang-javascript prettyprint-override"><code>var db,
      mongoose = require('mongoose');
  
  ...
  
  function initDB() {
    var options = {
      server: {
        auto_reconnect: true,
        socketOptions : {
          keepAlive: 1
        }
      }
    };
  
    mongoose.connect('mongodb://localhost/mydatabase', options);
  
    db = mongoose.connection;
  
    db.on('error', console.error.bind(console, 'Error:'));
  }
  
  ...
  
  MyClass.prototype.query = function(model, criteria, callback) {
    db.once('open', function () {
      model.find(criteria).exec(function(err, docs) {
        callback(err, {}, docs);
      });
    });
  };
  </code></pre>
  
  <p>What am I missing here? Any help would be greatly appreciated!</p>