Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback was already called async parallel

Hi I am trying to use the Async module to retrieve two users and do some processing after they have both been retrieved however I keep getting the error message: Callback was already called. Below is the code i currently have:

app.get('/api/addfriend/:id/:email', function(req, res) {
    var id = req.params.id;
    var friendEmail = req.params.email;
    async.parallel([
            //get account
            function(callback) {
                accountsDB.find({
                    '_id': ObjectId(id)
                }, function(err, account) {
                    console.log(id);
                    if (err || account.length === 0) {
                        callback(err);
                    }
                    console.log(account[0]);
                    callback(null, account[0]);
                });
            },
            //get friend
            function(callback) {
                accountsDB.find({
                    'email': friendEmail
                }, function(err, friend) {
                    console.log(friendEmail);
                    if (err || friend.length === 0 || friend[0].resId === undefined) {
                        callback(err);
                    }
                    console.log(friend[0]);
                    callback(null, friend[0].resId);
                });
            }
        ],

        //Compute all results
        function(err, results) {
            if (err) {
                console.log(err);
                return res.send(400);
            }

            if (results === null || results[0] === null || results[1] === null) {
                return res.send(400);
            }

            //results contains [sheets, Friends, Expenses]
            var account = results[0];
            var friend = results[1];
            if (account.friends_list !== undefined) {
                account.friends_list = account.friends_list + ',' + friend;
            }
            else {
                account.friends_list = friend;
            }
            // sheetData.friends = results[1];
            accountsDB.save(
                account,
                function(err, saved) {
                    if (err || !saved) {
                        console.log("Record not saved");
                    }
                    else {
                        console.log("Record saved");
                        return res.send(200, "friend added");
                    }
                }
            );

        }
    );
});

Any help would be appreciated.

like image 422
sazap10 Avatar asked Dec 17 '14 08:12

sazap10


2 Answers

Add else statement to your code, because if you get error, your callback executes twice

if (err || account.length === 0) {
  callback(err);
} else {
  callback(null, account[0]);
}
like image 171
Oleksandr T. Avatar answered Nov 20 '22 19:11

Oleksandr T.


The docs from async actually say:

Make sure to always return when calling a callback early, otherwise you will cause multiple callbacks and unpredictable behavior in many cases.

So you can do:

return callback(err);
like image 27
ctindel Avatar answered Nov 20 '22 18:11

ctindel