Search code examples
javascriptnode.jsmongodbnode-mongodb-native

node-mongodb-native, callback, scope and TypeError


Here is a little story.

Once upon a time, a little project wanted to use node-mongodb-native. However, it was very shy, and it wanted to use a wrapper object to hide behind it.

var mongodb = require( 'mongodb' ),
    Server = mongodb.Server,
    Db = mongodb.Db,
    database;

var MongoModule = {};

MongoModule.setup = function() {
    // Create a mongodb client object
    var client = new Db( this.config.databaseName,
        new Server(
            this.config.serverConfig.address,
            this.config.serverConfig.port,
            this.config.serverConfig.options
        ),
        this.config.options
    );

    // Open the connection!
    client.open( function( err, db ) {
        if ( err ) throw err;
        database = db;
        console.log( 'Database driver loaded.' );
    });
};

The setup method was a way to get the little project started. It was being called when the application was running.

To try itself a little out, the little project added a wrapper method for the collection method of node-mongodb-native.

MongoModule.collection = function() {
    database.collection.apply( this, arguments );
};

But then, the little project found out that this method didn't work. It didn't understand why!

// In the client.open callback:
db.collection( 'pages', function( e, p ) {
    // no error, works fine
});

// in the same callback:
MongoModule.collection( 'pages', function( e, p ) {
    // error :(
});

The error was the following, even though the little project doesn't think it's related. His best friend Google didn't turn up any useful result but an old fixed bug.

TypeError: Cannot read property 'readPreference' of undefined
    at new Collection (/home/vagrant/tartempion/node_modules/mongodb/lib/mongodb/collection.js:56:92)
    at Object.Db.collection (/home/vagrant/tartempion/node_modules/mongodb/lib/mongodb/db.js:451:24)
    at Object.MongoModule.collection (/home/vagrant/tartempion/core/databases/mongodb.js:27:25)
    at proxy [as collection] (/home/vagrant/tartempion/node_modules/ncore/lib/core.js:116:51)
    at Object.module.exports.getIndex (/home/vagrant/tartempion/pies/page/model.js:4:17)
    at proxy [as getIndex] (/home/vagrant/tartempion/node_modules/ncore/lib/core.js:116:51)
    at Object.module.exports.index (/home/vagrant/tartempion/pies/page/controller.js:7:20)
    at callbacks (/home/vagrant/tartempion/node_modules/express/lib/router/index.js:272:11)
    at param (/home/vagrant/tartempion/node_modules/express/lib/router/index.js:246:11)
    at pass (/home/vagrant/tartempion/node_modules/express/lib/router/index.js:253:5)

PS: if you want a failing file, here is a gist.


Solution

  • You need to apply the method collection in the context of the database object rather than the MongoModule object:

    database.collection.apply( database, arguments );