I'm trying to display an array via the jade view engine after fetching data from mongodb with a mongoose scheme unfortunately the array doesn't reach the user due to an error code 500 recieved when adressing the page , I think this happens due to a timing issue (the page gets rendered before the code has a chanse to get the data) because the data is present in the code and i am able to log it to the console however the error code appears in the console prior to the data being logged ... does anyone have an idea of how I can resolve this issue ?
The node code:
let validateUserToSession = function(req,res,next){
let PlayLst=[];
if(req.session && req.session.user)
{
Schemes.User.findOne({eMail:req.session.user.eMail},function(err,user) {
if(!user)
{
req.session.reset();
res.render('login',{message:{error:'Please log in ...',status:0}})
}
else
{
PlayLst=[];
Schemes.PlayList.findOne({ref:user.id},function(err,playlist) {
if(err)
{PlayLst=['No Tracks were chosen...']}
else{
if(playlist!==null){
playlist.favSongs.forEach(function(element) {
PlayLst.push(element.Artist + " " + element.Track);
console.log(PlayLst);
});
}}})
res.locals.PlayLst = PlayLst;
res.locals.user=user;
res.render('index',{ title: 'Index' });
}
});
}
else{
res.render('login',{message:{error:'Please log in ...',status:0}})
}
}
/* GET home page and preform validation on user */
router.get('/index',validateUserToSession);
Pug (Jade...) code :
h2 Logged in as : #{session.user.nName}
p Name: #{session.user.fName} #{session.user.lName}
p E-Mail: #{session.user.eMail}
if (session.PlayLst.length != undefined && session.PlayLst.length>0)
ul Favorite Songs :
each track in session.PlayLst
li = track
Client Side Error:
\index.jade:21 19| p Name: #{session.user.fName} #{session.user.lName} 20| p
E-Mail: #{session.user.eMail} > 21| if (session.PlayLst.length != undefined
&& session.PlayLst.length>0) 22| ul Favorite Songs : 23| each track in
session.PlayLst 24| li = track Cannot read property 'length' of undefined
TypeError: C:\SomePath\index.jade:21
19| p Name: #{session.user.fName} #{session.user.lName}
20| p E-Mail: #{session.user.eMail}
> 21| if (session.PlayLst.length != undefined && session.PlayLst.length>0)
22| ul Favorite Songs :
23| each track in session.PlayLst
24| li = track
via Gal Ratzkin
No comments:
Post a Comment