Reputation: 105
Im trying to write an if statement in Node.js with some mongoose query but the if statement does not get executed correctly.
So I'm doing this:
app.get('Job/GetJobs', function(req,res){
if(JobDB.Find()==null){
req.render('home.html');
}
else req.render('Job.html')
})
But the above code works if it was in java but not in Node.js because req.render('home.html'); gets executed before JobDB.find() finishes.
Upvotes: 0
Views: 657
Reputation: 7306
Most functions in Node.js are asynchronous, so you need to use a callback:
app.get('Job/GetJobs', function(req,res){
JobDB.find({}, function(err, result) {
if(!result) req.render('home.html')
else req.render('Job.html')
})
})
(you'll have to include some error handling there)
See the documentation: http://mongoosejs.com/docs/2.7.x/docs/finding-documents.html
Upvotes: 1