NodeJS 异步回调未触发

NodeJS async callbacks not firing

我正在使用 NodeJS 和 Async api 创建一个 api 函数,该函数 returns 一个故事列表。 get 可以是浅的(仅包含对象 ID 引用其他对象)或深的(所有 ID 引用都通过将 ID 替换为其引用的对象来解除引用)。浅获取工作正常,但是当我 运行 深拷贝时,它挂起。您可以在我的回调中看到我放置了 console.log(#) 来记录哪个回调被触发,但是 none 被触发了。

我觉得问题出在我是否误解了 async 如何处理 .each、.serial 和 .parallel 函数的回调函数参数。我需要一个在异步完成其所有任务后将被触发的函数,但是在每个操作(串行或并行完成)之后调用回调函数。

router.get('/stories', function(req, res, next) {
    var db = req.db,
        options = {
            deep : req.query.deep != null ? parseInt(req.query.deep) : false,
            offset : req.query.offset || 0,
            limit : req.query.limit || 0
        };

    Story.listStories(db, options, function(err, stories){
        if (!options.deep){
            res.json(new Response(err, stories));
            res.end();
        }else{
            if (err || stories == null){
                res.json(new Response(err, null));
                res.end();
                return;
            }

            async.each(stories,
                function(story, cb1){
                    var articles = [],
                        galleries = [];

                    async.series([
                        function(cb2){
                            async.parallel([
                                //Extract the story's articles and their outlets
                                function(cb3){
                                    async.each(story.articles,
                                        function(article_id, cb4){
                                            Article.getArticle(db, article_id, function(err, article){
                                                if (err){
                                                    cb4(err);
                                                    return;
                                                }

                                                Outlet.getOutlet(db, article.outlet, function(err, outlet){
                                                    if (err){
                                                        cb4(err);
                                                        return;
                                                    }

                                                    article.outlet = outlet;
                                                    articles.push(article);
                                                });
                                            });
                                        },
                                        function(err){console.log(4);
                                            if (err)
                                                cb3(err);
                                        });
                                }
                            ],
                            function(err){console.log(3); //Parallel callback
                                if (err)
                                    cb1(err);
                            });
                        },
                        function(cb2){
                            story.articles = articles;
                        }
                    ],
                    function(err){console.log(2);
                        if (err)
                            cb1(err);
                    });
                },
                function(err){console.log(1);
                    res.json(new Response(err, stories));
                    res.end();
                }
            );
        }
    });
});

您仅针对错误情况调用那些异步回调(cb1、cb2、cb3、cb4 等)。您还需要调用非错误案例。示例:

if (err) {
  return cb1(err);
}
cb1(null);   // or cb1()