I am just starting out with node.js and express.js and have a little questions I hope someone can answer.
Within my route I have the following code
router.get('/:slug', function(req, res) {
var subject = req.params.slug;
var subjectTitle ='';
var db = req.db;
db.collection('subjects').findOne({slug:subject}, function(err, result) {
console.log(result);
console.log(result.title);
subjectTitle = result.title;
});
res.render('subject', { title: subject});
});
I dont understand how I can access the result of the db call outside of the function that calls it.
Ideally I want to send result.title back as the subject into the render call.
Any help would be great
Thanks
Here is the answer.
router.get('/:slug', function(req, res) {
var subject = req.params.slug;
var subjectTitle ='';
var db = req.db;
db.collection('subjects').findOne({slug:subject}, function(err, result) {
console.log(result);
console.log(result.title);
res.render('subject', { title: result.title});
});
});