javascript - 数据未正确存储在购物车中
问题描述
- 我正在尝试获得另一项服务并且效果很好,但问题是,如何正确实现逻辑以正确生成购物车项目,我想在每次函数调用时将购物车的数量增加一个,但是因为它似乎它不起作用。
- 在
Postman
我总是得到所选数据的 ID 时,这就是我想要的。 - 那么如何在
totalQty
每次调用函数时生成变量以增加 1 呢?
这是通过另一项服务检索信息并尝试将其融入现有功能AddProduct()
。
app.get('/add-to-cart/:id', async (req, res) => {
try {
let id = req.params.id;
let cart = new Cart(req.body.cart ? req.body.cart : {});
const { data } = await axios.get('http://localhost:4200/products');
const singleProduct = await data.find((product) => product._id === id)
cart.addProduct(singleProduct, singleProduct._id);
req.body.cart = cart;
res.redirect('/');
console.log(req.body.cart);
}
catch (error){
console.log(error)
}
});
这是添加购物车的逻辑:
module.exports = function Cart(oldCart) {
this.productItems = oldCart.productItems || {};
this.totalQty = oldCart.totalQty || oldCart.totalQty==0.00;
this.totalPrice = oldCart.totalPrice || oldCart.totalPrice==0.00;
this.addProduct = function(item, id) {
let storedItem = this.productItems;
storedItem = this.productItems = {item: item, qty: 0, price: 0};
if (!storedItem){
storedItem = this.productItems = {item: item, qty: 0, price: 0};
console.log("stored is empty")
}
else{
console.log("Stored item full!")
storedItem = this.productItems = {item: item, qty: storedItem.qty, price: storedItem.price}
storedItem.qty++;
storedItem.price = storedItem.item.price * storedItem.qty;
this.totalQty ++;
this.totalPrice += storedItem.price;
}
}
this.generateArray = function () {
let arr = [];
for (let id in this.productItems) {
arr.push(this.productItems);
}
return arr;
}};
这就是我得到的console.log
解决方案
每次测试总数量为 2 的原因可能是因为您调用了一次 add product ,然后在下一行,您再次在控制台日志中调用了 cart.addProduct 。您应该存储结果然后控制台日志以防止双重添加
cart.addProduct(singleProduct, singleProduct._id);
console.log(cart.addProduct(singleProduct, singleProduct._id)); // Remove this line
改成这个
const response = cart.addProduct(singleProduct, singleProduct._id);
console.log('Cart response: ', response);
来自评论问题的更新 1:
您一遍又一遍地获得总数量 1 的原因,我认为这是因为您没有更新 oldCart 的数量。因此,您应该在 addProduct 之后返回 storedItem 并更新 oldCart。
.
更新 2 忽略更新 1,我看到了问题。totalQty 1 一遍又一遍的原因是因为您在 API 调用上创建了新的购物车。
更新 3 更新了您的 addProduct 代码:
我注意到代码中有一些错误,例如storedItem = this.productItems = {item: item, qty: 0, price: 0};
module.exports = function Cart(oldCart) {
this.productItems = oldCart.productItems || {};
this.totalQty = oldCart.totalQty || oldCart.totalQty==0.00;
this.totalPrice = oldCart.totalPrice || oldCart.totalPrice==0.00;
this.addProduct = function(item, id) {
let storedItem = {item: item, qty: 0, price: 0};
// Update here
if (this.productItems){
console.log("Stored item full!")
storedItem = {item: item, qty: storedItem.qty, price: storedItem.price}
}
console.log("totalQty: ",this.totalQty);
console.log("Quantity 1: ",storedItem.qty);
storedItem.qty++;
console.log("Quantity 2: ",storedItem.qty);
storedItem.price = storedItem.item.price * storedItem.qty;
this.totalQty ++;
this.totalPrice += storedItem.price;
}
}
this.generateArray = function () {
let arr = [];
for (let id in this.productItems) {
arr.push(this.productItems);
}
return arr;
}};
推荐阅读
- extjs - 在 ExtJS 可编辑网格中编辑后如何将脏指示符(红色指示符)添加到网格中的字段?
- odoo - 在销售订单行中计算新的 price_unit
- css - 如何让我的角落色带正确渲染?
- python - 如何在表单中设置默认值
- php - AMQPException 库错误:表对于缓冲区来说太大
- powershell - 尝试通过 Powershell 从 GoDaddy 网站下载文件夹时出现权限错误
- sql - 我想要最多多列
- wordpress - 每当我重新启动时,docker-composer wordpress 都会询问安装
- docker - 从私有注册表中提取的 Docker 给出了本地错误:tls:坏记录 MAC
- mysql - 如何创建一个 MYSQL 触发器,在更新另一个表时更新产品的库存水平,该表具有该产品的多个实例?