I am new to node js and mongoose.I have the below code in node js. I wanted to assign the return userData record by the mongoose to variable user which is outside the callback.
var user = null;
User.findOne({$and: [{"_id": advisorId}, {"role": "advisor"}]}, {firstName:1,lastName:1, '_id':0},
function(err,userData) {
user = userData;
});
Once I have the return result in user variable I can directly pass it to the jade view as follow:
TrackSession.find({'advisor_id' : advisorId},fields,function(err, chatHistoryData) {
var jade = require('jade');
var html = jade.renderFile(appRoot+'/views/generatePDFHTML.jade', {'chatHistoryData': chatHistoryData,
'selectedOptions':selectedOptions,
'advisor':user,
'tableHeaders':tableHeaders
});
console.log(html); return false;
});
But when I am trying to do this I am getting null as the value of the user variable. Is there any specific solution to achieve this?