Hi i am a newbie on nodejs I was trying to implement a project in which we have to event management site..where we are adding event to the page for a specific user.
Now the problem is i am getting stuck at this point:-
exports.getDashBoard=function(req,res){
var eventList=[];
for(var i=0;i<req.user.invites.length;i++)
{
Event.find({_id:req.user.invites[i]},function(err,events){
if(err)
{
console.log(err);
}
else
{
eventList.push(events[0]);
console.log(eventList);
// shows value inside the array as wanted
}
});
}
console.log(eventList);
// shows null value why? the variable is in the scope as its declaration
res.render('dashboard');
};
Explanation: I created a function in which i have the variable eventList declared and initialized every time the function gets called. I used this variable eventList Inside a inner function to update its value and concatenate to the previous values.. The console.log shows that the eventList is getting updated as wanted.But when i try to use this variable outside the the inner function is doenst work and i get a empty array as initialized The scope of the variable is local is in the main function and is visible inside the inner funciton but when i use it after the inner funciton ie outside the for loop the array shows null value What to do?
the image below:- I created a function in which i have the variable eventList declared and initialized everytime the function gets called. I used this variable eventList Inside a inner function to update its value and concatenate to the previoys values.. The console.log shows that the eventList is getting updated as wanted.But when i try to use this variable outside the the inner function is doenst work and i get a empty array as initialized The scope of the variable is local is in the main function and is visible inside the inner funciton but when i use it after the inner function the vanishes
What to do?