首先,我很抱歉,因为我使用了翻译,所以写作可能不自然。
我刚开始编码,我不知道什么更好或不更好。我将感谢你的意见和帮助。
我目前正在实现购物中心的购物车功能。
如果用户当前购物车的状态如下:
let currentCart = [
{
id: "AAAAA",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 1",
},
quantity: 1,
},
{
id: "AAAAA",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 2",
},
quantity: 1,
},
];
当用户尝试将新项目添加到购物车时,如下所示:
let newCart = [
{
id: "AAAAA",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 1",
},
quantity: 1,
},
{
id: "BBBBB",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 1",
},
quantity: 1,
},
];
我想要的结果如下:
let result = [
{
id: "AAAAA",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 1",
},
quantity: 2, // The cart that already exists adds quantity.
},
{
id: "AAAAA",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 2",
},
quantity: 1,
},
{
id: "BBBBB",
option: {
delivery: "pickup",
date: "2023.01.06",
request: "request 1",
},
quantity: 1, // Cart that doesn't exist will be newly added.
},
];
如何实现这些功能?
我尝试了以下代码:
router.post("/addToCart", auth, async (req, res) => {
const rootProductDoc = await Product.findOne({
_id: req.body.productId,
}).exec();
User.findOne({ _id: req.user._id }, (err, result) => {
let existingOptionStr = result.cart.map((cartItem) => {
return JSON.stringify(cartItem.option);
});
let duplicateOption = req.body.option.filter((createdOption) => {
return existingOptionStr.includes(JSON.stringify(createdOption));
});
if (duplicateOption) {
for (let i = 0; i < duplicateOption.length; i++) {
User.updateMany(
{ _id: req.user._id, "cart.option": duplicateOption[i] },
{
$inc: { "cart.$.quantity": 1 },
},
{
new: true,
},
(err, result) => {
if (err) {
return res.status(400).json({ success: false, err });
} else {
return res.status(200).send(result.cart);
}
}
);
}
}
});
});
根据上面的代码,更新之前包含在购物车中的信息效果良好。
但我目前正忙于执行剩下的案例。
我想同时运行与下面代码相同的函数。
User.updateMany(
{
_id: req.user._id,
},
{
$push: {
cart: {
rootProductDoc: rootProductDoc,
id: rootProductDoc.id,
quantity: 1,
option: req.body.option,
},
},
},
{
new: true,
},
(err, result) => {
if (err) {
return res.status(400).json({ success: false, err });
} else {
return res.status(200).send(result.cart);
}
}
);
我应该怎么做才能得到上述预期结果?
首先,检查购物车,物品是否存在。如果存在,则更新商品的数量;如果商品不存在,则在购物车中添加新商品。
例如:
router.post("/addToCart", auth, async(req, res) => {
const rootProductDoc = await Product.findOne({
_id: req.body.productId
}).exec()
User.findOne({
_id: req.user._id
}, (err, result) => {
let existingItem = result.cart.find((item) => {
return item.id === rootProductDoc.id && item.option === req.body.option
})
if (existingItem) {
// item exists in cart, so update the quantity of the item
User.updateMany({
_id: req.user._id,
"cart._id": existingItem._id
}, {
$inc: {
"cart.$.quantity": 1
}
}, {
new: true
},
(err, result) => {
if (err) {
return res.status(400).json({
success: false,
err
})
} else {
return res.status(200).send(result.cart)
}
}
)
} else {
// item does not exist, so add to the cart
User.updateMany({
_id: req.user._id
}, {
$push: {
cart: {
rootProductDoc: rootProductDoc,
id: rootProductDoc.id,
quantity: 1,
option: req.body.option
}
}
}, {
new: true
},
(err, result) => {
if (err) {
return res.status(400).json({
success: false,
err
})
} else {
return res.status(200).send(result.cart)
}
}
)
}
})
})
如果你遇到任何问题。请让我知道