11

I'm using Async module in Node.JS to keep track of my asynchronous calls. However, I'm getting an error - "Callback already called." Can someone help me out here?

async.each(data['results'], function(result, done) { 
    if (result['twitter_id'] !== null) { //Isolate twitter handle
        var param = { "user.screen_name": result['twitter_id']}
        db.test4.find( param, function(err, users) {
            if( err ) {
                return done(err);
            } else if (!users) {
                res.send("No user found");
            } else {
                users.forEach( function(Result) { 
                    twitter_ids.push(Result);
                    //console.log(Result);
                    done();
                });
            }
        });
    }
}, function(err) {  
    if (err) {
        throw err
    }
    res.send(twitter_ids);
});
2
  • 1
    you're calling done() in each iteration of the forEach loop. you should move done outside of that loop and it'll work. you should also make sure done() gets called in the "else if" branch too. Commented Jun 27, 2014 at 22:43
  • 1
    I have never used the Async module, but just looking at your code, it looks like you are firing done() (i.e. your callback) multiple times in the loop Commented Jun 27, 2014 at 22:43

1 Answer 1

12

You're calling res.send("No user found"); each time you fail to load. However you can fail to load multiple times.

The solution is to put all your response code in the final callback, not in the each callback.

async.each(data['results'], function(result, done) { 
    if (result['twitter_id'] !== null) { //Isolate twitter handle
        var param = { "user.screen_name": result['twitter_id']}
        db.test4.find( param, function(err, users) {
            if( err ) {
                done(err);
            } else if (!users) {
                done(new Error("No user found"));
            } else {
                users.forEach( function(Result) { 
                    twitter_ids.push(Result);
                    //console.log(Result);
                });
                done();
            }
        });
    } else {
      done();
    }
}, function(err) {  
    if (err) {
        return next(err);
    }
    res.send(twitter_ids);
});
Sign up to request clarification or add additional context in comments.

4 Comments

While I agree that this is also an issue, I believe the error ("Callback already called.") comes from looping over done() in the else. The OP probably doesn't want to execute the callback (done) multiple times.
ahh you're right. It's both, but that's the one that looks like it throw the error, fixing now.
Yeah, didn't catch that haha. Thanks y'all! :)
You should send return at last instead of sending in each condition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.