I'm trying to create a function that will query a mongo db and then return the result, then render it on a page.
I'm just currently trying to console.log the objects to find out why this isn't working:
var getMarkets = function (marketID) {
MongoClient.connect('mongodb://localhost:27017/test', function(err, db){
db.collection("new-test-2", function (err, collection) {
collection.find({"ID": parseInt(marketID, 10)}).toArray(function(err, items) {
console.log(items);
return items;
});
});
});
};
router.get('/markets/:marketid', function(req, res, next) {
var marketobj = getMarkets(req.params.marketid);
console.log(marketobj);
res.render('api', { title: 'API', marketid: marketobj });
});
The log nested inside the function works fine, but then the log inside the routing object returns undefined. What's going on here? I have a feeling it has to do with asynchronous callbacks but I'm not able to reason about it.
Thanks a ton.