这是我的产品模型.
const featureHeaderSchema = new Schema({
header: {
type: String,
required: true,
},
});
const featureSchema = new Schema({
title: {
type: String,
required: true,
},
parentHeader: { type: Schema.Types.ObjectId, ref: "product.featureHeaders" }, // not sure if this is correct
parentFeature: { type: Schema.Types.ObjectId, ref: "product.features" }, // not sure if this is correct
});
const productSchema = new Schema(
{
title: {
type: String,
required: true,
},
thumbnail: String,
description: String,
manufacture: { type: Schema.Types.ObjectId, ref: "user" },
featureHeaders: {
type: [featureHeaderSchema],
},
features: {
type: [featureSchema],
},
},
{ timestamps: true }
);
const productModel = mongoose.model("product", productSchema);
我想通过id找到一个产品,并使用populate获取parentHeader的标题和parentFeature的标题.
我try 通过产品ID获取产品信息,并填充到parentHeader和parentFeature上,这样我就可以在输出中显示它们的标题.
我try 了这个查询:
const output = await productModel
.findById(newProduct._id)
.populate("features.parentFeature")
.exec();
但我得到这个错误:MissingSchemaError: Schema hasn't been registered for model "product.features".