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