Question: Hello i am trying to create a simple client side cart, in my controller when the page loads i define the array objects that will hold my items, if they are undefined or have a length of 0 i set them to '[]' by default:

```
$scope.cart = JSON.parse($window.localStorage.getItem('cart')) || [];
$scope.cart.items = $scope.cart.items || [];

```

This is the function that adds the item to the cart:

```
$scope.addItem = function(item) {

    if (item.quantity > 0)
    {
        var cartItem = {
            id: item.id,
            description: item.class + ' item' + (item.quantity > 1 ? 's' : '') + ' to ' + $scope.details.name,
            quantity: item.quantity
        }
        // Adding item to cart array
        $scope.cart.items.push(cartItem)
        // Saving cart to storage
        $window.localStorage.setItem('cart', JSON.stringify($scope.cart));

        // Checking to see if data has indeed been stored in localstorage
        console.log(JSON.parse($window.localStorage.getItem('cart')));
    }

}

```

Now my cart in storage always turns up as empty, there were times i played around with the code and got it to work(dunno what i did) but when i reloaded the page everything got cleared.

Answer: 
You are initializing `cart` as array but then assigning properties to it. 

It should be an object:

```
$scope.cart = JSON.parse($window.localStorage.getItem('cart')) || {};

```

Also before you can `push` to an array within `cart` that array needs to be defined.

Simplest might be to have all the properties available in one object first:

```
var newCart = {
    tickets:[],
    items:[]    
}
$scope.cart = JSON.parse($window.localStorage.getItem('cart')) || newCart ;

```
You should check if there is already a value stored as

```
if(!$window.localStorage.getItem('cart'))
{
// Saving cart to storage
        $window.localStorage.setItem('cart', JSON.stringify($scope.cart));
}

```