This is my app.route for .get, & .post. Rather than repeat the MongoClient.connect for each function I want to wrap the app.route with it instead. But it doesn’t seem to work.
Can anyone explain where I am going wrong?
module.exports = function (app) {
MongoClient.connect(MONGODB_CONNECTION_STRING, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, db) {
app.route('/api/books')
.get(function (req, res) {
db.collection('books').find({}).toArray ((err, docs) => {
if(docs) {
console.log(docs); //nothing is consoled
} else {
console.log("no docs"); //nothing is consoled
}
});
})
.post(function (req, res){ })
});
};