Tuesday 14 February 2017

mongodb returns Cursor, I loop through and add objects into an array, how do I send the array client side without third party libraries?

this is what I have done so far: router.get('/', function(req, res, next) { mongo.connect('mongodb://localhost:27017/interview_prac', function(err, db) { var item = [] if (err) throw err; else { var nettuts = db.collection('nettuts') nettuts.find({gender: 'm'}, function(err, data) { if (err) throw err if (data) { data.forEach(function(myDoc) { item.push(myDoc) }) db.close() res.render('index', {data: item}); } }) } }) }); I want to send item to the client side, however, it get sends before items are added. I did look into promise, but that requires adding third party library. I don't want to add third party library. How would I send it without having using third party libraries?

Submitted February 14, 2017 at 08:57PM by lostwhitewalker

No comments:

Post a Comment