2

I have tested my code offline and now I have put it on a domain and hosted, the cart system does not work and comes up with the error

   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)

I've tried if cart == null then cart = [] but it doesn't work. My page is a food/takeaway menu that when you click a food item the price is added to cart but it is completely disabled.

heres my 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();

Please Help, its the last stage of this project. thank you in advance.

risingmoon77
  • 91
  • 10
  • from where you are calling addItemToCart function? – AL-zami Mar 10 '18 at 22:18
  • The `this` in an event handler will be the dom element that is clicked. You need to bind your handler to the right `this`. See [this question](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – James Mar 10 '18 at 23:07

1 Answers1

2

Inserted this code cart = cart || []; in line 21 of the script. hope this helps someone down the line.

risingmoon77
  • 91
  • 10