イベントを扱うアプリケーションがあります。ユーザーがイベントを作成するには、イベントを作成するためにログインする必要があります。イベント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});
// }
}
})
});
あなたのエラーが 'events'ルートハンドラにある場合は、あなたが既に投稿したものに加えて、またはそれに加えて投稿してください。 – mscdex
ありがとう、詳細を掲載しました。 – illcrx
あなたの 'console.log(allEvents)'は何を表示し、 'console.log(typeof allEvents)'は何を表示していますか?後者が 'object'を表示する場合、 'console.log(Array.isArray(allEvents))'は何を表示しますか?私が手 – mscdex