I am trying to get results from my "Books.find" and push it into my books array. I want to then res.send it.
I suspect this has something to do with some kind of asynchronous and scope rubbish.
What's the solution?
This is currently my 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']);
}
function getDataByUsername(username) {
function addBookArray(result) {
books.push(result);
return result;
}
var Books = mongoose.model('Book');
Books.find({'username': username}).exec(function (err, result) {
addBookArray(result);
});
}
for(var usernames in name) {
getDataByUsername(name[usernames]);
}
console.log(books);
res.send(books);
});
}