我收到此错误:
预订验证失败:用户:值投射到ObjectID失败
"{ "userId": "5c4479b51318a01df4a5a5dc", "username": "imran", "iat": 1548003730, "exp": 1548090130 }"
路径“用户”
这是我的代码:
if (isValidBooking(booking, foundRental)) {
booking.user = user;
booking.rental = foundRental;
foundRental.bookings.push(booking);
booking.save(function(err) {
if (err) {
return res.status(422).send(err);//getting error on this line
}
foundRental.save();
User.update(
{ _id: user.id },
{ $push: { bookings: booking } },
function() {}
);
return res.json({ startAt: booking.startAt, endAt: booking.endAt });
});
答案 0 :(得分:0)
我猜你的错误发生在行
booking.user = user;
此外,我猜测无论对象booking
是什么,属性user
应该是一个ObjectId,而在此方法中,您将为其提供完整的User
对象。尝试将该行重写为:
booking.user = user.userId;