Here is a short story.
Once a small project wanted to use node-mongodb-native . However, it was very shy, and he 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() {
var client = new Db( this.config.databaseName,
new Server(
this.config.serverConfig.address,
this.config.serverConfig.port,
this.config.serverConfig.options
),
this.config.options
);
client.open( function( err, db ) {
if ( err ) throw err;
database = db;
console.log( 'Database driver loaded.' );
});
};
The method setupwas a way to launch a small project. It is called when the application starts.
To try a little, a small project added a wrapper method for the method collection node-mongodb-native.
MongoModule.collection = function() {
database.collection.apply( this, arguments );
};
But then a small project found out that this method does not work. He did not understand why!
db.collection( 'pages', function( e, p ) {
});
MongoModule.collection( 'pages', function( e, p ) {
});
The mistake was the following, although the small project did not think it was connected. His best friend Google did not get any useful result, except for the old bug fixed.
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 bad file, here is the gist .