after nodejs update i get this error: Cast to ObjectId failed for value "600204c674086624b9de76e2" at path "classId" for model "Message"
after nodejs update i get this error: Cast to ObjectId failed for value "600204c674086624b9de76e2" at path "classId" for model "Message"
这是我的代码:
async function allMessage(roomId){
await Message.find({ classId: roomId }).sort({ date: -1 }).limit(4)
.populate('userId', 'name')// just select name
.exec(function(allMessage){
console.log(allMessage);
})
}
这是我的模型结构:
userId:{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
classId:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Classroom'
},
message:{
type: String,
required: true
},
date:{
type: Date,
default: Date.now
}
但我收到此错误:对于模型“Message”的路径“classId”中的值“600204c674086624b9de76e2”,转换为 ObjectId 失败
我尝试使用以下代码将 roomId 更改为 ObjectId:
mongoose.Types.ObjectId(roomId)
但我收到此错误:传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串
在更新 nodejs 之前它工作正常。我现在很困惑。我该怎么办?
尝试在您获得的字符串 ID 上使用 trim(),然后在查询中传递它
这是我的代码:
async function allMessage(roomId){
await Message.find({ classId: roomId }).sort({ date: -1 }).limit(4)
.populate('userId', 'name')// just select name
.exec(function(allMessage){
console.log(allMessage);
})
}
这是我的模型结构:
userId:{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
classId:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Classroom'
},
message:{
type: String,
required: true
},
date:{
type: Date,
default: Date.now
}
但我收到此错误:对于模型“Message”的路径“classId”中的值“600204c674086624b9de76e2”,转换为 ObjectId 失败
我尝试使用以下代码将 roomId 更改为 ObjectId:
mongoose.Types.ObjectId(roomId)
但我收到此错误:传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串
在更新 nodejs 之前它工作正常。我现在很困惑。我该怎么办?
尝试在您获得的字符串 ID 上使用 trim(),然后在查询中传递它