【发布时间】:2019-09-23 17:40:45
【问题描述】:
我一直在尝试在我的购物车中填充产品。 我的购物车型号是 -
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
]
}
【问题讨论】:
标签: javascript node.js mongodb mongoose populate