我正在嘗試將產品添加到 mongodb 的購物車中,但我撰寫的代碼不起作用。我已經設定了 if 陳述句來檢查所選產品的 id 是否包含在 items 陣列中,如果為 true,則 console.log('match found'); 在這個 if 陳述句中也是設定我的代碼不起作用的地方。你能告訴我更新產品數量的更好方法嗎?我正在撰寫代碼以完全按照此處顯示的檔案https://www.mongodb.com/docs/manual/reference/operator/update/inc/更新專案數量,所以我不知所措。
const express = require('express');
const Carts = require('../repo/carts');
const router = express.Router();
router.post('/cart/products', (req, res) => {
Carts.findById(req.session.cartId, (err, foundCart) => {
if (err) {
console.log(err); // This err is for the find by Id, not to the update function
}
if (foundCart) {
console.log(foundCart);
console.log(req.body.productId);
let check = foundCart.items.map((item) => item.id.toString());
console.log(check);
if (check.includes(req.body.productId)) {
console.log('MATCH FOUND');
Carts.updateOne(
{ _id:foundCart._id}, { _id: req.body.productId,
$inc: { quantity: 1 } }
);
} else {
console.log('no match')
Carts.updateOne(
{ _id: foundCart._id },
{
$push: {
items: {
_id: req.body.productId,
quantity: 1,
},
},
},
(err, updatedCart) => {
if (err) {
console.log(err);
}
}
);
}
} else {
if (!foundCart) {
const newCart = new Carts({
_id: req.session.cartId,
items: [],
});
newCart.save();
}
}
});
res.send('product added to cart!!');
});
module.exports = router;
陣列示例
items: [
{ quantity: 1, _id: '6356ffb3ece7e49784bfbd5d' },
{ quantity: 1, _id: '6356ffb3ece7e49784bfbd5d' },
{ quantity: 1, _id: '6356ff91ece7e49784bfbd5a' },
{ quantity: 1, _id: '6356ff75ece7e49784bfbd57' },
{ quantity: 1, _id: '63570003ece7e49784bfbd69' },
{ quantity: 1, _id: '63570003ece7e49784bfbd69' },
]
uj5u.com熱心網友回復:
試試這個邏輯:
router.post('/cart/products', async (req, res) => {
try {
let cart = await Carts.findById(req.session.cartId);
// Check if cart exists
if (!cart) {
// Create new cart
cart = await Carts.create({
_id: req.session.cartId,
items: [],
});
}
// Check if product is present
const productIndex = cart.items.findIndex(
(item) => item._id === req.body.productId
);
if (productIndex === -1) {
// Create new product
cart.items.push({
_id: req.body.productId,
quantity: 1,
});
} else {
// Update existing product
cart.items[productIndex].quantity = 1;
}
// Save updated cart
await cart.save();
res.status(200).send('product added to cart!!');
} catch (err) {
res.status(500).send('there was an error');
}
});
uj5u.com熱心網友回復:
我不太確定這一點,但看起來您可能對異步方法有疑問。
此方法既沒有等待也沒有回呼函式,因此行程將繼續(并且可能完成)而不等待它完成。
Carts.updateOne(
{_id: req.body.productId },
{ $inc: { quantity: 1 } }
);
https://mongoosejs.com/docs/api.html#query_Query-updateOne(如果你看那里的例子,它正在等待中)
另一方面,你應該得到這個塊:
Carts.updateOne(
{ _id: foundCart._id },
{
$push: {
items: {
_id: req.body.productId,
quantity: 1,
},
},
},
(err, updatedCart) => {
if (err) {
console.log(err);
}
}
);
在else塊內。因此,如果您找到帶有 id 的現有商品,您會嘗試更新它,但只有當您沒有找到它時,您才會將新商品推送到購物車中。
它應該看起來像這樣:
if (check.includes(req.body.productId)) {
console.log('MATCH FOUND');
Carts.updateOne(
{_id: req.body.productId },
{ $inc: { quantity: 1 } }
);
} else {
Carts.updateOne(
{ _id: foundCart._id },
{
$push: {
items: {
_id: req.body.productId,
quantity: 1,
},
},
},
(err, updatedCart) => {
if (err) {
console.log(err);
}
}
);
}
我希望這能解決問題!:D
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/524732.html
下一篇:傳播物件并添加標志node.js
