2016-11-27 97 views
0

我有一个节点/ express/ejs应用程序,当我转到页面时,它显示变量具有null值。这是路线。EJS变量显示为空

router.get("/events", isLoggedIn, function(req,res){ 
    User.findById(req.params.id).populate("moments").populate("events").exec(function(err,allEvents){ 
     if(err){ 
      console.log(err); 
      console.log("Ummm.... the database may be down?.......") 
     } else { 
      if (allEvents === null){ 
       res.redirect("/dashboard"); 
      } else { 
      console.log("Below are all the events pulled"); 
      console.log(allEvents) 
      res.render("eventsList", {events: allEvents}); 
      } 
     } 
    }); 
}); 

因此,事件是我传入“eventsList”的变量。渲染器上方的console.log显示事件,当我创建一个新事件时,它附加到该列表。然而,每次它进入/仪表板页面,就好像所有事件中都为空。

这里是在客户端代码:

<% events.forEach(function(events){ %> 

林gessing它是与if语句,但我不能弄明白。

回答

1

也许你没有指定userid参数在你的路线

router.get("/events/:userId", isLoggedIn, function(req,res){ 
console.log("userId: ", req.params.userId) 
User.findById(req.params.userId).populate("moments").populate("events").exec(function(err,allEvents){ 
    if(err){ 
     console.log(err); 
     console.log("Ummm.... the database may be down?.......") 
    } else { 
     if (allEvents === null){ 
      res.redirect("/dashboard"); 
     } else { 
     console.log("Below are all the events pulled"); 
     console.log(allEvents) 
     res.render("eventsList", {events: allEvents}); 
     } 
    } 
}) 
}); 
+0

这并没有工作,那部分代码已经被以前的工作,无论在哪里我CONSOLE.LOG的“allEvents”这表明空值?这个“if”陈述有什么不正确吗? – illcrx

+0

if语句没有问题;你确定它正确调用的路线吗? – Victorious

+0

请您发布'console.log(req.params)' – Victorious