首页 > 解决方案 > 数据未正确存储在购物车中

问题描述

这是通过另一项服务检索信息并尝试将其融入现有功能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

在此处输入图像描述

标签: javascriptvue.jspostman

解决方案


每次测试总数量为 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;
    }};


推荐阅读