this is my code:
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);
})
}
and this is my model structure:
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
}
but i get this error: Cast to ObjectId failed for value "600204c674086624b9de76e2" at path "classId" for model "Message"
I try to change roomId to ObjectId with this code:
mongoose.Types.ObjectId(roomId)
but i get this error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
befor updating the nodejs it works fine. I confused now. what should I do?
try using trim() on the string id you are getting and then pass it in the query