I'm trying to make a single connection to my MongoDB and store the response (database) I get back in a global variable, so I can re-use it in seperate JS file (like the separate files for my routes). I'm following this documentation as example: https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connection-pooling.
First try: var mongodb = require('mongodb'), MongoClient = mongodb.MongoClient, MongoURL = "my_mongodb_url:port/database_name", global.db;
MongoClient.connect(MongoURL, function(err, database) {
db = databse;
console.log(db); // shows stuff
}
console.log(global.db); // shows undefined
After some research I found a possible fix for the problem: create a global variable in Node with a global prefix. But it's still not working...
Second try:
var mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient,
MongoURL = "my_mongodb_url:port/database_name",
global.db;
MongoClient.connect(MongoURL, function(err, database) {
global.db = databse;
console.log(db); // shows stuff
}
console.log(global.db); // shows undefined
The code above is all put in the same file [app.js]. I think it has to do with the scope of MongoClient. But I'm not sure. Is there a way to make this work?