skip to Main Content

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);

2

Answers


  1. 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,
    },
    
    Login or Signup to reply.
  2. I had this issue as well. Turned out I had an unused import of the schema within a file and once removed the error went away. The previous solutions also work however if you find that you dont have any unused schema imports.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search