未捕获的TypeError:无法读取属性'push'的null

时间:2018-03-10 22:13:51

标签: javascript jquery html null anchor

我已经离线测试了我的代码,现在我把它放在一个域上并托管,购物车系统无法运行并出现错误

   cart.js:22 Uncaught TypeError: Cannot read property 'push' of null
    at Object.shoppingCart.addItemToCart (cart.js:22)
    at HTMLAnchorElement.<anonymous> (index2b.html:469)
    at HTMLAnchorElement.dispatch (jquery-3.3.1.min.js:2)
    at HTMLAnchorElement.y.handle (jquery-3.3.1.min.js:2)

我试过如果购物车== null然后购物车= []但它不起作用。我的页面是食品/外卖菜单,当您点击食品时,价格会被添加到购物车中,但它会被完全禁用。

继承我的JS

    //*******************************************************//
//** shopping cart fucntions

var shoppingCart = {};
shoppingCart.cart = [];
shoppingCart.Item = function(name, price, count) {
            this.name = name
            this.price = price
            this.count = count
        };


 shoppingCart.addItemToCart = function(name, price, count) {
    for (var i in this.cart) {
        if (this.cart[i].name === name) {
            this.cart[i].count += count;
            this.saveCart();
            return;
        }
    }
    var item = new this.Item(name, price, count);
    this.cart.push(item);
    this.saveCart();
};


 shoppingCart.removeItemFromCart = function(name) { // removes one item
            for (var i in this.cart) {
                if (this.cart[i].name === name) {
                    this.cart[i].count --;
                    if (this.cart[i].count === 0) {
                        this.cart.splice(i, 1);
                    }
                    break;
                }
            }   
            this.saveCart();

        };


 shoppingCart.removeItemFromCartAll = function(name) {// removes all item name
        for (var i in this.cart) {
            if (this.cart[i].name === name){
                this.cart.splice(i,1);
                break;
            }
        }
        this.saveCart();
    };


 shoppingCart.clearCart = function() {
            this.cart = [];
            this.saveCart();
        };      


 shoppingCart.countCart = function() { // -> return total couNt
    var totalCount = 0;
    for (var i in this.cart) {
        totalCount += this.cart[i].count;
    }

    return totalCount;
};

 shoppingCart.totalCart = function() { // -> return total coSt
        var totalCost = 0;
        for (var i in this.cart){
            totalCost += this.cart[i].price * this.cart[i].count;
        }
        return totalCost.toFixed(2);
    };


 shoppingCart.listCart = function() {   // -> array of item
    var cartCopy = [];
    for (var i in this.cart) {
        var item = this.cart[i];
        var itemCopy = {};
        for (var p in item) {
            itemCopy[p] = item[p];
        }
        itemCopy.total = (item.price * item.count).toFixed(2);
        cartCopy.push(itemCopy);
    }
    return cartCopy;
};

 shoppingCart.saveCart = function() {
            localStorage.setItem("shoppingCart", JSON.stringify(this.cart));
        };

 shoppingCart.loadCart = function() {
            this.cart = JSON.parse(localStorage.getItem("shoppingCart"));
        };


    shoppingCart.loadCart();

请帮助,这是该项目的最后阶段。提前谢谢你。

1 个答案:

答案 0 :(得分:1)

在脚本的第21行插入此代码cart = cart || [];。希望这可以帮助有人下线。