-1

Taking inspiration from this answer https://stackoverflow.com/a/21571589/9036255 I decided to copy this into my own code. I've found that it doesn't give any response.

function getCallback(callbackTrash) {
    var Books = mongoose.model('Book');
    Books.find({'username': name[usernames]}, function (err, result) {
        callbackTrash(result);
    });
}

getCallback(function(result) {
    books.push(result);
});

books is just an array. When I console log it later on, it returns "[]"

If I change books.push to console.log, it correctly logs all of the results.

My question is how to get the result to be pushed into the books array?

I have no experience with asynchronous things. What's a practical answer without a steep learning curve?


Here's my greater context:

exports.timeline = function(req, res) {
    var Followers = mongoose.model('Follow');
    Followers.find({'follower': req.user.username}, function(err, followerResult) {
        var name = [req.user.username];
        var books = [];

        function addName(username) {
            name.push(username);
        }

        for(var user in followerResult) {
            addName(followerResult[user]['followed']);
        }

        for(var usernames in name) {
            function getCallback(callbackTrash) {
                var Books = mongoose.model('Book');
                Books.find({'username': name[usernames]}, function (err, loadOfBollocks) {
                    callbackTrash(loadOfBollocks);
                });
            }

            getCallback(function(result) {
                books.push(result);
            });
        }

        console.log(books);
        res.send(books);
    });
}
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Techius
  • 9
  • 5
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Carcigenicate Sep 28 '18 at 12:36
  • 3
    I did read the question. That link contains how to deal with async code. If you don't understand it, it's a good read and introduction to the topic. There's many answers that give different perspectives. No need to be rude when people are trying to help you. – Carcigenicate Sep 28 '18 at 13:56

1 Answers1

0

I think that your code is correct. But:

  • where did you initialize "books" ?
  • Where did you console log it ?

EDIT

Ok i see now. You put you console and res.send in the wrong place. Those lines are executed before your loop (for) process is finished That's why you have an empty array for "books". Maybe you need to executed these codes only when you have finished to loop in the "name" array. Here is the code:

exports.timeline = function(req, res) {
    var Followers = mongoose.model('Follow');
    Followers.find({'follower': req.user.username}, function(err, followerResult) {
        var name = [req.user.username];
        var books = [];

        function addName(username) {
            name.push(username);
        }

        for(var user in followerResult) {
            addName(followerResult[user]['followed']);
        }

        let count = 0;
        for(var usernames in name) {
            function getCallback(callbackTrash) {
                var Books = mongoose.model('Book');
                Books.find({'username': name[usernames]}, function (err, loadOfBollocks) {
                    count++;
                    callbackTrash(loadOfBollocks);
                    if(count == name.length){
                        console.log(books);
                        res.send(books);
                    }
                });
            }

            getCallback(function(result) {
                books.push(result);
            });
        }
    });
}

Hope it helps !

hathija
  • 85
  • 1
  • 9