I'm facing difficulties when attempting a simple mongoDB query from my express app:
app.js
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var mongourl = ''; // omitted on SO
var MongoClient = require('mongodb').MongoClient;
var dbInstance;
MongoClient.connect(mongourl, function(err, db) {
db.on('open', function(){
dbInstance = db;
})
});
app.get('/', routes.index(dbInstance));
http.createServer(app).listen(app.get('port'), function(){
});
routes/index.js
exports.index = function(db){
return function(req, res){
}
};
Am I correct in understanding that the parameter for 'exports.index' is a database instance? If so, why am I unable to perform db.getCollectionNames()
?
How can I effectively work with the database instance in my route?