以下代码从供应商集合中的雇员数组中拉出雇员
await new VendorManager()
.update(
{ employee: mongoose.Types.ObjectId(id) },
{ $pull: { employee: mongoose.Types.ObjectId(id) } }
);
字符串已转换为**mongoose.Types.ObjectId**
,但仍然出现错误
error: "Cast to ObjectId failed for value "{ employee: 5c80c16e0c253823fc44332a }" at path "_id" for model "Vendor""
我已经测试过更新功能
mongoose.Types.ObjectId.isValid(id)
它也返回true
。
供应商架构
name: {
type: String
},
employee: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Employee'
}],
created_by: {
type: String
},
updated_by: {
type: String
},
答案 0 :(得分:1)
仅查看错误消息,似乎更新功能的第一个参数应该是ObjectId,而不是像这样的数据:
{ employee:ObjectId }
因此,当您尝试将上述对象转换为ObjectId时,会出现超级有用的错误:
error: "Cast to ObjectId failed for value "{ employee: 5c80c16e0c253823fc44332a }" at path "_id" for model "Vendor""