Shaharyar ali
Shaharyar ali

Reputation: 1

Cast to ObjectId failed for value \"\" (type string) at path \"category\" because of \"BSONTypeError\

I have the Cast to error in JSON response when Adding the Product Details in the & when adding category showing Error.

const mongoose = require("mongoose"); const { ObjectId } = mongoose.Schema;

{

category: { type: ObjectId, ref: "Category", },

};

Except category Every detail is adding in the mongodb table

Upvotes: 0

Views: 1012

Answers (1)

Bùi Anh Khoa
Bùi Anh Khoa

Reputation: 90

You should try mongoose.SchemaTypes.ObjectId

@Prop({ type: mongoose.SchemaTypes.ObjectId, ref: 'ExercisePlan' })
    category: string;

And if you are trying to aggregate with _id so you should try

$match: {
    category: new Types.ObjectId(inputId),
},

Upvotes: 0

Related Questions