I am new to Node and Mongo, I was trying to connect to a mongo database in one file and export the database handler to many other files, so that I need not to connect to the database in all files which need a connection to it. Here is how I attempted to do it
// db.js
var client = require(mongodb).MongoClient
var assert = require(assert)
var url = mongodb://localhost:27017/test
client.connect(url, (err, db) => {
assert.equal(err, null)
module.exports = db
})
After exporting the db handler, I tried to access methods on it in another file as follows
var db = require(./db)
console.log(db.collection(col))
but it throws a TypeError, saying that db.collection is not a function. How can I access the methods on db handler in other files?