Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”

前端 未结 13 2076
再見小時候
再見小時候 2020-11-30 01:24

I am new to node.js, so I have a feeling that this will be something silly that I have overlooked, but I haven\'t been able to find an answer that fixes my problem. What I\'

13条回答
  •  爱一瞬间的悲伤
    2020-11-30 02:09

    Had the same problem, I just coerced the id into a string.

    My schema:

    const product = new mongooseClient.Schema({
        retailerID: { type: mongoose.SchemaTypes.ObjectId, required: true, index: true }
    });
    

    And then, when inserting:

    retailerID: `${retailer._id}`
    

提交回复
热议问题