Saturday, 6 January 2018

Call res.render after queries to db have ocurred

Hi, I have a web that has a mongodb database with two collections: revistas and notas. To each revita (magazine in spanish) corresponds a bunch of notas (stories in spanish). Using mongoose I can create an object that holds the info that I want to pass to the res.render function. However, I don't understand how I can call the res.render functions only after all the queries have been completed and the object is complete.The code:router.get('/', function(req,res){ var revistasobj = {}; Revista.find().sort({_id : -1}).limit(5).exec(function (err, revistas){ for(var i = 0; i < revistas.length; i++){ Nota.find({revista : revistas[i]._id}).exec(function(error, notas){ revistasobj[notas[0].mes] = notas; }) } }) }) Thanks in advanceEDIT: forgot to tell I'm using express.

Submitted January 06, 2018 at 05:30AM by tandroide

No comments:

Post a Comment