Lion789
Lion789

Reputation: 4482

Applying async nodejs

How to apply async series to this code... I am having trouble with the querying and using the results correctly... I need to take the user.friendStatus which is a list of all the friends a user have in the Friend collection and then, take each id of the friend and find the user info from the User collection...

UPDATE: Changed async.series to async.waterfall so I can pass one result to the next function. However, I am unsure how you call and pass the results from one to the next...

Here is my code:

    exports.contactList = function(req, res) {

    async.waterfall([
    function(callback) {
        var friends = [];
        Friend.findOne({userId: req.signedCookies.userid}, function(err, users) {
            if(err) {throw err};
            for(var i = 0; i < users.friendStatus; i++) {
                if(users.friendStatus[i].status === 3) { friends.push(users.friendStatus[i])};  
            }
        });
            callback(null, friends);
        },

        function(callback) {
            var friendsinfo = [];
            for(var i = 0; i < friends.length; i++) {
                User.findbyID({_id: friends[i].fuID}, function(err, users) {
                    if (err) { throw err};
                    friendsInfo.push(users[i], friends[i].favorites);
                });
            }
            callback(null, friendsinfo);
        }
    ],

    function(err, results) {
        res.render('contactList', {title: 'Weblio', Friends: friendsinfo});
    });

};

Upvotes: 0

Views: 398

Answers (1)

alfonsodev
alfonsodev

Reputation: 2754

Here is an example of async.waterfall from https://github.com/caolan/async#waterfall I added more comments to me make it clear

async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        // arg1 is equal 'one'
        // arg2 is equal 'two
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});

[edit] applied to your code...(notice is just add friends param to the second function)

[edit] better indentation, and change results var to friendsinfo in the last callback

 exports.contactList = function(req, res) {

async.waterfall([
    function(callback) {
        var friends = [];
        Friend.findOne({userId: req.signedCookies.userid}, function(err, users) {
            if(err) {throw err};
            for(var i = 0; i < users.friendStatus; i++) {
                if(users.friendStatus[i].status === 3) { friends.push(users.friendStatus[i])};  
            }
        });
        callback(null, friends);
    },

    function(friends, callback) {
        var friendsinfo = [];
        for(var i = 0; i < friends.length; i++) {
            User.findbyID({_id: friends[i].fuID}, function(err, users) {
                if (err) { throw err};
                friendsInfo.push(users[i], friends[i].favorites);
            });
        }
        callback(null, friendsinfo);
    }
],

function(err, friendsinfo) {
    res.render('contactList', {title: 'Weblio', Friends: friendsinfo});
});

};

Upvotes: 1

Related Questions