I have a problem and add item

it only adds if I give f5

at Scope.$scope.addItem (viewProduct.js:46)

$scope.addItem = function(item) {
    item.qtd = 1;
    $cart.addItem(item);
    $state.go('client.checkout',{id :  $scope.table});
 
};

ionic.bundle.js:25642 TypeError: Cannot read property ‘items’ of null
at addItem (cart.js:29)

      for(var index in cart.items){
            itemAux = cart.items[index];

            if (itemAux.id == item.id){
                itemAux.qtd = item.qtd + itemAux.qtd;
                itemAux.subtotal = calculateSubTotal(itemAux);
                exists = true;
                break;
            }
        }

Hi,
It is a syntax error .
I guess you are pushing items an array .
So use the javascript inbuilt push() function .

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");

and what is $cart.addItem(); Is that an Angular service …?

i think you initialised your cart items variable with “null”, but you should use an empty array.

  i needs to check your code for this    
  <div class="item-accordion text-center wrap no-padding-top padding-bottom" collection-repeat="p in items "  ng-click="addItem(p)" >
angular.module('starter.services')
    .service('$cart',['$localStorage',function($localStorage){

        var key = $localStorage.getObject('table', '[]');

        var cartAux = $localStorage.getObject(key);


        if (!cartAux){
            initCart();
        }

        this.clear = function(){
           initCart();
        };


        this.get = function(){
            return $localStorage.getObject(key);
        };

        this.getItem = function(i){
            return this.get().items[i];
        };

        this.addItem = function(item){
            var cart = this.get(),itemAux,exists = false;

            for(var index in cart.items){
                itemAux = cart.items[index];

                if (itemAux.id == item.id){
                    itemAux.qtd = item.qtd + itemAux.qtd;
                    itemAux.subtotal = calculateSubTotal(itemAux);
                    exists = true;
                    break;
                }
            }

            if (!exists){
                item.subtotal = calculateSubTotal(item);

                cart.items.push(item);
            }

            cart.total = getTotal(cart.items);

            $localStorage.setObject(key,cart);

        };

        this.removeItem = function(i){
            var cart = this.get();

            cart.items.splice(i,1);

            cart.total = getTotal(cart.items);

            $localStorage.setObject(key,cart);
        };

        this.updateQtd = function(i,qtd){
            var cart = this.get(),
                itemAux = cart.items[i];

            itemAux.qtd = qtd;

            itemAux.subtotal = calculateSubTotal(itemAux);

            cart.total = getTotal(itemAux);

            $localStorage.setObject(key,cart);
        };


        this.setCupom = function(code,value){
          var cart = this.get();

            cart.cupom = {
                code  : code,
                value : value
            }

            $localStorage.setObject(key,cart);
        };

        this.removeCupom = function(){
            var cart = this.get();

            cart.cupom = {
                code  : null,
                value : null
            }

            $localStorage.setObject(key,cart);
        };

        this.getTotalFinal = function(){
            var cart = this.get();

            return cart.total - (cart.cupom.value || 0);
        }

        function calculateSubTotal(item){
            return item.price * item.qtd;
        };

        function getTotal(items){
            var sum = 0;

            angular.forEach(items,function(item){
                sum += item.subtotal;
            });

            return sum;
        };

        function initCart(){
            $localStorage.setObject(key,{
                items: [],
                total: 0,
                cupom:{
                    code  : null,
                    value : null
                }
            });
        };


    }]);


$scope.table =  $localStorage.getObject('table', '[]');
  $scope.addItem = function(item) {
        item.qtd = 1;
      $cart.addItem(item);
       $state.go('client.checkout',{id :  $scope.table});
     
    };

like i said “cartItems” are not initialised in your service. your are directly using cartItems.push, cartItems.splice but cartItems is not initialised as an array.

A little hint: do no prefix own services with $ --> this should be reserved for angular internal stuff.

greets

I excuse my ignorance but how do I do that then I am new in ionic

this has nothing to do with ionic. simple javascript stuff.

But i see you are initialising your object correctly.

But

var key = $localStorage.getObject('table', '[]');

Are you sure that getObject returns the singe key to access your localstorage object later? i think it will return the Object stored under ‘table’ or an empty array.

Key should be a simple string

this would be correct? however he is not returning the items?

$scope.table = $localStorage.getObject(‘table’, ‘[]’);
$scope.addItem = function(items) {

  meuCarrinho=   new cart($scope.table);

 
};

angular.module(‘starter.services’)
.service(‘cart’,[’$localStorage’,function($localStorage){

function cart(idMesa){

   this.idMesa = idMesa;
   var _cart = {
   items: []

   };
  var key = idMesa;
  var cartAux = $localStorage.getObject(key);


    if (!cartAux){
        initCart();
    }

    this.clear = function(){
       initCart();
    };


    this.get = function(){
        return $localStorage.getObject(key);
    };

    this.getItem = function(i){
        return this.get().items[i];
                         console.log(items);
    };

    this.addItem = function(item){
        var cart = this.get(),itemAux,exists = false;

        for(var index in cart.items){
            itemAux = cart.items[index];

            if (itemAux.id == item.id){
                itemAux.qtd = item.qtd + itemAux.qtd;
                itemAux.subtotal = calculateSubTotal(itemAux);
                exists = true;
                break;
            }
        }

        if (!exists){
            item.subtotal = calculateSubTotal(item);

            cart.items.push(item);
        }

        cart.total = getTotal(cart.items);

        $localStorage.setObject(key,cart);

    };

    this.removeItem = function(i){
        var cart = this.get();

        cart.items.splice(i,1);

        cart.total = getTotal(cart.items);

        $localStorage.setObject(key,cart);
    };

    this.updateQtd = function(i,qtd){
        var cart = this.get(),
            itemAux = cart.items[i];

        itemAux.qtd = qtd;

        itemAux.subtotal = calculateSubTotal(itemAux);

        cart.total = getTotal(itemAux);

        $localStorage.setObject(key,cart);
    };


    this.setCupom = function(code,value){
      var cart = this.get();

        cart.cupom = {
            code  : code,
            value : value
        }

        $localStorage.setObject(key,cart);
    };

    this.removeCupom = function(){
        var cart = this.get();

        cart.cupom = {
            code  : null,
            value : null
        }

        $localStorage.setObject(key,cart);
    };

    this.getTotalFinal = function(){
        var cart = this.get();

        return cart.total - (cart.cupom.value || 0);
    }

    function calculateSubTotal(item){
        return item.price * item.qtd;
    };

    function getTotal(items){
        var sum = 0;

        angular.forEach(items,function(item){
            sum += item.subtotal;
        });

        return sum;
    };

    function initCart(){
        $localStorage.setObject(key,{
            items: [],
            total: 0,
            cupom:{
                code  : null,
                value : null
            }
        });
    };
}

return cart;

}]);