Reputation: 392
After googling alot on how to setup MongoDb in an Express/NodeJs application i thought i knew how to implement it in a decent/performant way. I initiate my mongodbConnection in the WWW file that Express is providing me so there is only 1 connection being opened while the app is started.
WWW
!/usr/bin/env node
var debug = require('debug')('myapp');
var app = require('../app');
var mongoConnection = require('../mongo/mongoconnection.js');
mongoConnection.init(function(error){
// Start the application after the database connection is ready
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
});
In my mongoConnection.js file i work out the actual initialisation of the database and i export the database variable.
mongoconnection.js
var mongodb = require('mongodb');
var bson = mongodb.BSONPure;
var MongoClient = mongodb.MongoClient;
// Initialize connection once
module.exports.init = function(callback)
{
MongoClient.connect("mongodb://localhost:27017/capturemongo", function(err, database) {
if(err) throw err;
module.exports.db = database;
callback(err);
});
};
However when i try to acces the database variable from my mastermanager.js file the db variable is undefined.
mastermanager.js
var mongoConnection = require('./mongoconnection.js');
var db = mongoConnection.db;
module.exports.getCollection = function( callback){
db.collection('masters', function(err, collection){
if(!err){
callback(null, collection);
}
else{
console.log("getcolelction error " + err);
callback(err);
}
});
};
module.exports.findAll = function(callback) {
this.getCollection(function(error, collection) {
if( error ){
callback(error);
}
else {
collection.find().toArray(function(error, results) {
if( error ) {
callback(error);
console.log('finderror ' + error);
}
else{
console.log(results);
callback(null, results);
}
});
}
});
};
I really cannot figure out why the variable is undefined there. When i set console.log() statements in the init of mongoconnection i clearly see that de database variable is set!
Any help would be much appreciated!
Upvotes: 0
Views: 94
Reputation: 1771
You could try something like this:
www
...
let _db = null;
MongoClient.connect('mongodb://localhost:27017/capturemongo', (err, db) => {
_db = db;
});
app.use(function(req, res, next) {
res.locals.db = _db;
next();
});
...
routes/index.js
...
router.get('/', function(req, res) {
res.locals.db; //Do something with the db connection
});
...
Upvotes: 0
Reputation: 1095
I don't see where mastermanager.js is required, but module.exports.db is set in your callback, which is probably executing long after mastermanager.js is required.
Just guessing, but you might try moving the declaration of db until you're actually going to use it, like this:
module.exports.getCollection = function( callback){
var db = mongoConnection.db;
db.collection('masters', function(err, collection){
if(!err){
callback(null, collection);
}
else{
console.log("getcolelction error " + err);
callback(err);
}
});
};
Upvotes: 1