0
我似乎得到2個錯誤.....廣東話設置頭後,他們被送到ALSO CASTError:演員爲[的ObjectId]失敗
這裏是路線:
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};
//Create the event itself in the database, event will return the actual database event.
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
console.log(event);
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();
//Save the event ID in the user document
console.log(event);
User.findByIdAndUpdate(
req.user._id,
{$push: {events:{"ObjectId": event._id}}},
{save: true, upsert: true, new: true},
function (err,newEventData){
if(err){
console.log("error at saving the id ..." + err)
res.redirect("/dashboard");
} else {
console.log(newEventData);
}
}
);
//Add the Event ID to the User model
console.log (owner);
};
});
res.redirect('events');
});
這裏是從Mongoose返回值的console.log的輸出以及錯誤。
The id of the user 583f30b1e5e7e376502762f5
Below are all the events pulled{ _id: 583f30b1e5e7e376502762f5,
username: 'asdf',
__v: 0,
favoriteMoments: [],
favoriteEvents: [],
likeEvents: [],
likeMoments: [],
friends: [],
moments: [],
events: [],
categories: [] }
{ __v: 0,
title: 'asdf',
description: 'asdf',
_id: 583f3175b6a3b376a515c146,
comments: [],
photos: { link: '', date: null },
moments: [],
category: [ '' ],
owner: { id: 583f30b1e5e7e376502762f5, username: 'asdf' } }
{ __v: 0,
title: 'asdf',
description: 'asdf',
_id: 583f3175b6a3b376a515c146,
comments: [],
photos: { link: '', date: null },
moments: [],
category: [ '' ],
owner: { id: 583f30b1e5e7e376502762f5, username: 'asdf' } }
{ id: 583f30b1e5e7e376502762f5, username: 'asdf' }
error at saving the idCastError: Cast to [ObjectId] failed for value "[{"ObjectId":"583f3175b6a3b376a515c146"}]" at path "events"
events.js:141
throw er; // Unhandled 'error' event
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/var/www/familysite.com/node_modules/express/lib/response.js:719:10)
at ServerResponse.location (/var/www/familysite.com/node_modules/express/lib/response.js:836:15)
at ServerResponse.redirect (/var/www/familysite.com/node_modules/express/lib/response.js:874:18)
at /var/www/familysite.com/routes/eventRoute.js:67:29
at /var/www/familysite.com/node_modules/mongoose/lib/model.js:3388:16
at /var/www/familysite.com/node_modules/mongoose/lib/model.js:3388:16
at /var/www/familysite.com/node_modules/kareem/index.js:207:48
at /var/www/familysite.com/node_modules/kareem/index.js:127:16
at nextTickCallbackWith0Args (node.js:419:9)
at process._tickCallback (node.js:348:13)
下面是用戶模式的架構:
const userSchema = new mongoose.Schema({
username: String,
password: String,
nickname: String,
firstName: String,
middleName: String,
lastName: String,
address: String,
city: String,
state: String,
phone: Number,
birthday: Date,
birthplace: String,
userCover: String,
categories: Array,
events: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
moments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Moments"
}],
friends: [{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}],
likeMoments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Moments"
}],
likeEvents: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
favoriteEvents: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}],
favoriteMoments: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Moments"
}]
})
我已經越來越沒有地方用這個演員的問題,現在我有2個錯誤,這似乎很奇怪......讓在這個非常沮喪點和不確定去哪裏。
最後,我有需要創建一個事件的路由,將它保存到創建它的用戶的事件ID,然後轉到/event
頁面並顯示每個事件的數據。
您還有其他問題,如果發生錯誤,您將重定向兩次,這就是爲什麼您會收到「標頭髮送」錯誤。修復數據庫錯誤可以修復其他問題.... *現在*,但是當數據庫查詢失敗時,您仍然會遇到一個錯誤,它會一直困擾着您。 – adeneo
@adeneo哦!感謝您指出了這一點。所以你說我退出到'/ dashboard'然後它退出並進入'/ events'吧?你將如何處理這種情況,你想在回調中推送不同的頁面?你會回來嗎?或者,這同樣糟糕。 – illcrx