0

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?

6
  • 1
    why don't you put the render code inside the findOne() callback ? Commented Dec 10, 2015 at 6:19
  • Why are you passing in 'user' into the callback? I think your hiding 'user' at the higher scope. Commented Dec 10, 2015 at 6:39
  • Opps it was by mistake , you can now check it I have edited the question. Commented Dec 10, 2015 at 6:49
  • @SarathNair: I have edited the question can you check the code once again. Actually, I want data with the user details. As unfortunately it is not supporting Relations mapping how could I get the data of the user by advisorId. Commented Dec 10, 2015 at 6:51
  • 1
    In your edited version, there is a chance that the find() callback of "TrackSession" executes after findOne() callback of "User" and the rendering would work as expected. But it is still unstable, sometimes it might not work. You should put TrackSession inside of the findOne() callback, to be sure. Commented Dec 10, 2015 at 7:03

1 Answer 1

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); return false;
    });
});

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.