开发Facebook克隆时接受好友请求接口遇CastError问题求助
问题:Facebook克隆项目接受好友请求接口CastError错误
错误详情
CastError: Cast to ObjectId failed for value "{ user: '62c1f76928dca4af2ec95010', isAdmin: true, iat: 1667490411, exp: 1667494011 }" (type Object) at path "matches" for model "User"
相关代码
接受好友请求路由代码
router.post("/user/acceptMatchrequest", verifyToken, async (req, res) => { const recipientId = req.user const senderId = req.body.sender const updatedSender = await User.findOneAndUpdate( { _id: senderId, matches: { $nin: [recipientId] } }, { $push: { matches: recipientId } }, { new: true } ) const updatedRecipient = await User.findOneAndUpdate( { _id: recipientId, matches: { $nin: [senderId] } }, { $push: { matches: senderId }, }, { new: true } ); if (updatedRecipient) { const updatedMatchRequest = await MacthRequest.findOneAndUpdate( { sender: senderId, recipient: recipientId, }, { $set: { status: 'accepted' }, $push: { friendshipParticipants: [senderId, recipientId] }, }, { new: true } ); const updatedRequests = await MacthRequest.find({ recipient: req.user, status: 'pending', }); res.status(200).send({ updatedRequests: updatedRequests, updatedUserFriendList: updatedRecipient.matches, }); } })
好友请求Schema
const mongoose = require("mongoose"); const matchRequestSchema = new mongoose.Schema( { sender: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, recipient: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, status: { type: String, }, friendshipParticipants: { type: [mongoose.Schema.Types.ObjectId] } }, { timestamps: true, } ); module.exports = mongoose.model("matchRequest", matchRequestSchema);
用户Schema
const mongoose = require("mongoose"); const UserSchema = new mongoose.Schema({ email: { type: String, required: true, unique: true }, password: { type: String, required: true, }, matches: { type: [mongoose.Schema.Types.ObjectId], ref: 'User' }, { timestamps: true }); module.exports = mongoose.model('User', UserSchema)
问题分析与修复方案
错误根源:recipientId被赋值为req.user,但req.user是包含user(用户ID)、isAdmin等字段的完整对象,并非单纯的ObjectId字符串。mongoose尝试将整个对象转换为ObjectId时失败,触发CastError。
修复步骤
- 提取正确的用户ID:将
const recipientId = req.user改为const recipientId = req.user.user,因为req.user.user才是真正的用户ObjectId字符串。 - 统一使用正确ID:所有需要用户ID的查询、更新操作,都使用提取后的
recipientId替代req.user对象。
修复后的路由代码
router.post("/user/acceptMatchrequest", verifyToken, async (req, res) => { // 提取有效用户ID const recipientId = req.user.user const senderId = req.body.sender const updatedSender = await User.findOneAndUpdate( { _id: senderId, matches: { $nin: [recipientId] } }, { $push: { matches: recipientId } }, { new: true } ) const updatedRecipient = await User.findOneAndUpdate( { _id: recipientId, matches: { $nin: [senderId] } }, { $push: { matches: senderId }, }, { new: true } ); if (updatedRecipient) { const updatedMatchRequest = await MacthRequest.findOneAndUpdate( { sender: senderId, recipient: recipientId, }, { $set: { status: 'accepted' }, $push: { friendshipParticipants: [senderId, recipientId] }, }, { new: true } ); const updatedRequests = await MacthRequest.find({ recipient: recipientId, status: 'pending', }); res.status(200).send({ updatedRequests: updatedRequests, updatedUserFriendList: updatedRecipient.matches, }); } })
额外检查
- 确认
verifyToken中间件解析JWT后,req.user中的user字段确实是有效的ObjectId字符串。 - 确保
req.body.sender传递的也是合法的用户ObjectId字符串,避免重复出现类型转换错误。
内容的提问来源于stack exchange,提问作者Joshua Ndeleva
相关产品推荐
相关产品推荐

