我暂时停止了这个项目的工作,现在我回来了,我收到了一个以前从未收到过的错误: '类型错误:架构配置无效:
Bt
不是路径 category
处的有效类型。请参阅 [site] 以获取有效架构类型的列表。'
这是我的代码中导致问题的部分。我不明白为什么会发生变化,因为我没有修改代码。
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);
根据您的问题,您正在将类别引用到产品架构中,因此在引用 Mongoose 中的另一个架构时,您将使用 mongoose.Schema.Types.ObjectId 而不是 mongoose.Types.ObjectId。
category: {
type: mongoose.Schema.Types.ObjectId, // Make sure this line is correct
ref: "Category",
required: false,
},