我一直在尝试在购物车中填充产品。 我的购物车型号为-
const ProductSchema = new mongoose.Schema({
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product'},
quantity: Number
});
const CartSchema = new mongoose.Schema({
userId: mongoose.Schema.Types.ObjectId,
products: [ProductSchema]
});
我正试图获得像这样的购物车价值-
let getCart = async (userId) => {
let res = await Cart.find({ userId: userId }).populate('products.product')
return res;
};
输出-
{
userId: xyz,
products:[
product: null,
quantity:1
]
}
预期结果-
{
userId: xyz,
products:[
product: {
name: 'product name',
description:'',
...
},
quantity:1
]
}
答案 0 :(得分:0)
您只需要填充products
,然后选择product
即可从填充的数组中显示。
答案 1 :(得分:-1)
我猜仅仅是填充产品就够了。
let getCart = async (userId) => {
let res = await Cart.find({ userId: userId }).populate('products')
return res;
};