我正在尝试根据id获取数据,以下是我的模型productsorders productsOrder
import mongoose from 'mongoose';
const { ObjectId, String, Number } = mongoose.Schema.Types;
const OrderSchema = new mongoose.Schema({
user: {
type: ObjectId,
ref: "User"
},
products: [
{
quantity: {
type: Number,
default: 1
},
product: {
type: ObjectId,
ref: "Product"
}
}
],
email: {
type: String,
required: true
},
total: {
type: Number,
required: true
},
status: {
type: String,
required: true,
default: 'pending',
enum: ["pending", "delivered"]
}
},{
timestamps: true
});
export default mongoose.models.Order || mongoose.model("Order", OrderSchema);
产品型号:
import mongoose from 'mongoose';
const { String, Number } = mongoose.Schema.Types;
const ProductSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
productType: {
type: String,
required: true
},
sku: {
type: String,
unique: true
},
description: {
type: String,
required: true
},
mediaUrl: {
type: String,
required: true
}
},{
timestamps: true
});
export default mongoose.models.Product || mongoose.model('Product', ProductSchema);
对于所有订单,查询如下所示:
const orders = await Order.find()
.sort({ createdAt: 'desc' })
.populate({
path: "products.product",
model: "Product"
});
// console.log(orders)
res.status(200).json({ orders });
这个概念是在创建使用产品ID创建的订单时,我想根据订单产品ID获取产品。
通过这种方式显示
MongooseError [MissingSchemaError]:模式尚未注册模型“Product”。
我该如何解决这个问题?
白猪掌柜的
相关分类