Skip to content Skip to sidebar Skip to footer

Assign Mongoose Return Result To Node Js Variable

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

Solution 1:

The callback of the findOne() is asynchronous, it gets executed after you get to rendering the jade. The execution jumps to "TrackSession" before the user variable gets a new value.

You should put the var html = ... inside the callback.

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); returnfalse;
    });
});

Post a Comment for "Assign Mongoose Return Result To Node Js Variable"