2016-11-27 9 views
0

私はノード/エクスプレス/ 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はイベントを表示し、新しいイベントを作成すると、そのリストに追加されます。しかし、たぶん、すべてのイベントにnullがあるかのように/ダッシュボードページに移動します。ここで

クライアント側のコードです:

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

イムgessingそれは、if文とは何か、私はそれを把握するカント。

答えて

1

たぶん、あなたはあなたのルートでは動作しませんでした

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

をUSERIDパラメータを指定していない、コードの一部は、以前に私はそれを示し、「allEventsを」はconsole.log関係なく働いていましたヌル?その "if"ステートメントについて何か間違っていますか? – illcrx

+0

if else文に問題はありません。あなたはそれが正しく呼ばれたルートですか? – Victorious

+0

あなたは 'console.log(req.params)'を投稿できますか – Victorious

関連する問題