reactjsmongodbmongoosemongoose-schema

Mongoose Error: TypeError: Invalid schema configuration: `Bt` is not a valid type at path `category`


I took a break from working on this project and now that I am back, I receive an error that I never received before: 'TypeError: Invalid schema configuration: Bt is not a valid type at path category. See [site] for a list of valid schema types.'

This is the bit of my code that is causing problem. I don't understand why something changed as I didn't modify the code.


const ProductSchema = new Schema(
  {
    title: { type: String, required: true },
    description: String,
    price: { type: Number, required: true },
    images: [{ type: String }],
    category: {
      type: mongoose.Types.ObjectId,
      ref: "Category",
      required: false,
    },
    properties: { type: Object },
  },
  {
    timestamps: true,
  }
);

export const Product = models.Product || model("Product", ProductSchema);

Solution

  • As per your question you are referencing the category into product schema so when referencing another schema in Mongoose, you would use mongoose.Schema.Types.ObjectId instead mongoose.Types.ObjectId.

    category: {
      type: mongoose.Schema.Types.ObjectId, // Make sure this line is correct
      ref: "Category",
      required: false,
    },