2016-11-28 10 views
0

イベントを扱うアプリケーションがあります。ユーザーがイベントを作成するには、イベントを作成するためにログインする必要があります。イベントIDをuser.events配列に追加する必要があります。しかし、私はユーザーにイベントを保存するのが難しくなっていますが、これが主な問題です。私はイベントが作成された後にリダイレクトされている私の 'イベント'ルートで私の他のデータを取得します。私のモデルでは新しいイベントを作成中に、イベントIDを作成したユーザーにイベントIDを保存しようとしています...エラー

router.post("/event", isLoggedIn, function (req,res){ 
// get data from form and add to events array 
var title = req.body.title; 
var date = req.body.date; 
var description = req.body.description; 
var venue = req.body.venue; 
var photo = req.body.photo; 
var category = req.body.category; 
//get user data to save to the event. 
var owner = { 
    id: req.user._id, 
    username: req.user.username 
} 
var newEvent = {category: category, title: title, date: date, description: description, venue: venue, photos:{link: photo,date: date}, owner: owner}; 
Event.create(newEvent, function(err, event){ 
    if(err){ 
     console.log(err) 
    } else { 
     //This takes the event owner ID and saves it into the Event model 
     //event.owner.id = req.user._id; 
     //This takes the event username and saves it into the Event model 
     event.owner.username = req.user.username; 
     event.save(); 
     //console.log(event); 
     //Save the event ID in the user document 
     User.update({_id: event.owner.id}, function(err,savedData){ 
      savedData.events = {eventId: event._id}; 
      savedData.save(); 

     }) 
     //Add the Event ID to the User model 
     console.log (owner); 
    } 
}); 
res.redirect('events'); 

}) 

私は配列としてUser.eventsを持って、私はそれが問題ですが、私が手にエラーが「イベント」へのリダイレクト上にある場合、それはevents.forEachであることを私に語ったことを確認していません関数ではありません。ここ

はエラーを作成しているテンプレートのラインである:ここ

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

は「イベント」経路である:

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

あなたのエラーが 'events'ルートハンドラにある場合は、あなたが既に投稿したものに加えて、またはそれに加えて投稿してください。 – mscdex

+0

ありがとう、詳細を掲載しました。 – illcrx

+0

あなたの 'console.log(allEvents)'は何を表示し、 'console.log(typeof allEvents)'は何を表示していますか?後者が 'object'を表示する場合、 'console.log(Array.isArray(allEvents))'は何を表示しますか?私が手 – mscdex

答えて

1

allEventsので、普通のオブジェクトではない配列でありますforEach()などの配列関連のメソッドは使用できません。

オブジェクトのプロパティを反復処理する場合は、Objects.keys(allEvents).forEach(function(key) {を実行し、allEvents[key]を使用して各キーの値を取得します。それ以外の場合は、events.forEach(function(events){(と関連する})を完全に削除してください。

+0

だから、あなたがオブジェクトを反復処理することができません'forEach'私はそれを知りませんでしたが、ここでも明らかに学んでいます。 このコードをノードまたはejsテンプレートに入れる必要がありますか?私はあなたが 'Objects.keys(allEvents)'を持っているのを見ます。それはオブジェクトの配列を作成しますか? プレースホルダを使用せずに実際のコードがどのように見えるかを明確にするために、私はそれらに混乱しています。ありがとう。 – illcrx

+0

私はObjects.keys()の本当に良い例を見つけました。 [link](https://davidwalsh.name/object-keys)、あなたの例は非常に良いですmscdex、申し訳ありません、私はあなたがすぐに置いていたものを拾いませんでした! – illcrx

関連する問題