nodejs 更新后,我收到此错误:模型 "Message" 的路径 "classId" 处的值 "600204c674086624b9de76e2" 强制转换为对象 ID 失败



这是我的代码:

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
}

但是我得到这个错误:转换为objecd失败的值"600204c674086624b9de76e2"@ path "class "for model "Message">

我尝试用以下代码将roomId更改为ObjectId:

mongoose.Types.ObjectId(roomId)

但是我得到这个错误:传入的参数必须是一个12字节的字符串或24个十六进制字符的字符串

在更新nodejs之前,它工作得很好。我现在糊涂了。我该怎么办?

尝试在字符串id上使用trim(),然后将其传递到查询

相关内容

  • 没有找到相关文章

最新更新