最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS Async: Callback already called? - Stack Overflow

programmeradmin0浏览0评论

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

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);
});
Share Improve this question asked Jun 27, 2014 at 22:37 techalicioustechalicious 4411 gold badge6 silver badges15 bronze badges 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. – aembke 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 – Steve Commented Jun 27, 2014 at 22:43
Add a comment  | 

1 Answer 1

Reset to default 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);
});
发布评论

评论列表(0)

  1. 暂无评论