user2084028
user2084028

Reputation: 51

Control flow using the async library

I have an array of posts called top thread, and I am trying to retrieve an array of arrays, each array containing all the replies to a single post in the original array sans the best one. here is my code for getting the top thread

exports.topThread = function(req, res){
// gets the leaf id from URL and assigns to OPID
var OPID = req.params.id;
var thread = [];
// finds the post with id OPID from the db
titles.findOne({postID: OPID}, function (err, post) {
if (err) throw err; 


getBestThread(OPID, thread, function(result){

    branchList.getBranches(function(branches){

        // renders content with data retrieved from the post
        res.render('content', {title: post.title, content: post.body, OPID: post.postID, currentThread: result, branches: branches});
        console.log(result);

    });
    });
});

};

this part of the code works: result is an array of the posts I want. Now I want to pass each element of the result array into my getAllOtherReplies function

//calback to retrieve other replies for a parent comment
    getOtherReplies = function(parentID, callback){
        postdb.otherReplies(parentID, function(err, commentsArray){
            if (err) throw err;
            callback(commentsArray);
        })
    }

    getAllOtherReplies = function(threadArray, returnArray, callback) {
        async.each(threadArray, 
            function(value, callback) {
                getOtherReplies(value.commentID, function(commentsArray) {
                    console.log(value.commentID);
                    if (commentsArray)
                        returnArray.push(commentsArray);
                });
                callback();
            },
            function(err) {
                if (err) throw err;
            }
        );
        callback(returnArray);
    }

and here is the function in postsDB:

//retrieves other replies from db
    exports.otherReplies = function(parentID, callback){

        titles.find({type: "comment", parentID: parentID}).sort({hotness: -1}).toArray(function(err, result){
            if (err) throw err;
            if (result.length > 1)
                callback(result.splice(0,1));
            else callback(null);
        });
    };

now I try to modify my original code to call the getAllOtherReplies function:

    exports.topThread = function(req, res){
        // gets the leaf id from URL and assigns to OPID
        var OPID = req.params.id;
        var thread = [];
        var allOtherReplies = [];
        // finds the post with id OPID from the db
        titles.findOne({postID: OPID}, function (err, post) {
        if (err) throw err; 


        getBestThread(OPID, thread, function(result){
            getAllOtherReplies(result, allOtherReplies, function(returnArray){
                console.log(returnArray);

                    branchList.getBranches(function(branches){

                    // renders content with data retrieved from the post
                    res.render('content', {title: post.title, content: post.body, OPID: post.postID, currentThread: result, branches: branches});
                    console.log(result);

                });
            });
            });
        });
    };

and instead of returning an array of arrays of all replies for each comment in result, it return an array of arrays where each array is the replies to only the first comment in result, repeated n times where n is the number of posts in result. I know there is an async problem I'm missing, any help would be appreciated.

Upvotes: 0

Views: 170

Answers (1)

RayViljoen
RayViljoen

Reputation: 1331

Inside your async.each loop, getOtherReplies is adding commentsArray inside of a callback, yet you are calling the async callback immediately after getOtherReplies and not waiting for it's callback.

Moving the async callback into getOtherReplies would be a good start and might very well solve the problem.

getOtherReplies(value.commentID, function(commentsArray) {
    console.log(value.commentID);
        if (commentsArray) {
            returnArray.push(commentsArray);
        }
        callback();
    });

Upvotes: 1

Related Questions