gpt4 book ai didi

javascript - 填充对象 Mongoose 的嵌套数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:13 25 4
gpt4 key购买 nike

我一直在尝试将产品填充到我的购物车中。我的购物车型号是 -

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]
});

我正在尝试像这样获取购物车值(value) -

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
]
}

最佳答案

您只需填充 products 并从填充的数组中选择要显示的 product

关于javascript - 填充对象 Mongoose 的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55999223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com