I am making a simple task app using Nodejs. When the user enters the home page, I will send the user's tasks to the front end. Each "User" has an array of task ids to sort out which task is theirs. I just keep getting an empty array when I try to push each task out to the local array.
User.findById(req.session.passport.user, function(err, user){
if(err){
console.log(err);
res.redirect("/login");
} else {
var tasks = new Array(user.tasks.length);
for(var i = 0; i < user.tasks.length; i++){
Task.findById( user.tasks[i] , function(err, task){
if(err){
console.log(err);
}
if(!tasks){
console.log("Couldn't find the task: " + user.tasks[i] );
} else {
console.log("Found task: " + task); //Tasks are always found
tasks.push(task); //<=== Not pushing?
}
});
}
console.log(tasks); // <====this is alwayse EMPTY
res.render("app-mobile", {user: user, tasks: tasks});
}
});