【发布时间】:2022-10-13 14:00:01
【问题描述】:
我正在尝试使用 Mongoose 在我的 UserSchema 中更新一个名为 watched_movies_list 的对象数组。通过传递_id: req.body.id,利用给定用户ID的$push对象。但是,在尝试更新 watched_movies_list 字段时,我遇到了这个转换错误(如下)。
reason: CastError: Cast to ObjectId failed for value "{
title: 'Luck',
overview: 'Suddenly finding herself in the never-before-seen Land of Luck, the unluckiest person in the world must unite with the magical creatures there to turn her luck around.',
poster_path: '/1HOYvwGFioUFL58UVvDRG6beEDm.jpg',
original_title: 'Luck',
original_language: 'en',
id: 550,
release_date: '2022-08-05',
genre_ids: undefined
}" (type Object) at path "movie_meta_data" because of "BSONTypeError"
这是我的用户架构:
watched_movies_list: [{
movie_meta_data: {
type: Schema.Types.ObjectId,
ref: "MovieDataSchema"
},
rating: {type: Number}
}]
这是 POST 路线:
try {
const user = await User.findOneAndUpdate(
{_id: req.body.id},
{ "$push": { watched_movies_list: watchedMovie }});
res.status(200).json({
success: 'true',
user: user
})
} catch (err) {
res.status(400).json(err);
throw err;
}
【问题讨论】: