terpak
terpak

Reputation: 1151

NodeJS async callbacks not firing

I'm working with NodeJS and the Async api to create an api function that returns a list of stories. The get can be shallow (only contains Object ID's referencing other objects) or deep (all ID references are dereferenced by replacing the ID with the object referenced by it). The shallow get works fine, however when I run the deep copy, it hangs. You can see in my callbacks I placed console.log(#) to log which callback is fired, but none are fired.

I feel like the issue lies within if I'm mistaking how async handles the callback function parameter for the .each, .serial and .parallel functions. I need a function that will be fired once async completes all of its tasks, but the callback function is instead called after every operation each, serial or parallel completed.

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

Upvotes: 0

Views: 750

Answers (1)

Ben
Ben

Reputation: 5074

You're calling those async callbacks (cb1, cb2, cb3, cb4, and etc) only for error cases. you need to call for non-error cases also. Example:

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

Upvotes: 1

Related Questions