Callback findOne() tidak sinkron, dijalankan setelah Anda merender batu giok. Eksekusi melompat ke "TrackSession" sebelum variabel pengguna mendapatkan nilai baru.
Anda harus meletakkan var html =... di dalam panggilan balik.
var user = null;
User.findOne({$and: [{"_id": advisorId}, {"role": "advisor"}]},{firstName:1,lastName:1, '_id':0}, function(err,userData,user) {
user = userData;
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;
});
});