嵌套的async.each:“错误:已经调用了回调”

时间:2017-02-06 14:09:53

标签: javascript node.js each async.js

我遇到了嵌套float工作的麻烦,我不知道为什么我收到错误:async.each即使我已经正确放置了我的回调。

Error: Callback was already called

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:2)

如果符合条件,则在循环浏览next();后再次呼叫default_shift

 default_shift.week_days.map(function(day1) {
            default_shift2.week_days.map(function(day2) {
                if (condition)
                    next({error: 'The shifts overlap!'}); //The problem is here.
            });
});
next(); //If shifts overlap, next was already called.

解决问题的一种简单方法是添加标记,如果移位重叠,则忽略第二个。

var nextCalled = false;
default_shift.week_days.map(function(day1) {
    default_shift2.week_days.map(function(day2) {
        if (condition && !nextCalled){
            next({error: 'The shifts overlap!'});
            nextCalled = true;
        }
    });
});

if(!nextCalled)
   next();