我有UserSchema
和CreditSchema
const UserSchema = new Schema({
credit: {
type: Schema.Types.ObjectId,
ref: 'Credit'
}
})
const CreditSchema = new Schema({
userId: Schema.Types.ObjectId,
credit: {
type: Number,
default: 0
}
})
然后我做了这2个动作。
//save to credit
await new Credit({
userId: mongoose.Types.ObjectId('5a3e76ce914e1d1bd854451d'),
credit: 100
}).save()
已成功插入数据。
//then I try to populate credit
await User.find({}).populate('credit').exec()
我收到错误
(node:17858) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError: Cast to ObjectId failed for value "0" at path "_id" for model "Credit"
为什么呢?我在保存新的信用文件时,我以为我已经将字符串转换为ObjectId了?