title: Meteor Storing and Retrieving Custom Objects in Session
body: <p>I have a custom shopping cart object that I created and put it in the <code>lib</code> folder.</p>
  
  <pre><code>ShoppingCart = function ShoppingCart() {
    this.Items = new Array();
    this.grandTotal = 0.00;
  }
  
  ShoppingCart.prototype.addItem = function(Item){
    this.Items.push(Item);
    this.Items.sort();
    this.calculateTotal();
  }
  </code></pre>
  
  <p>I initialized the shopping cart and store it as <code>Session.set('shoppingCart')</code> during the page <code>created</code> phase.</p>
  
  <pre><code>Template.loginStatus.created = function() {
    Session.set('loginShown',false);
    if(!Session.get('shoppingCart')){ //set default if session shopping cart not exist
      var cart = new ShoppingCart();
      Session.setDefault('shoppingCart',cart);
    }
  </code></pre>
  
  <p>Then when user click add item to cart, it will trigger this logic:</p>
  
  <pre><code>var cart = Session.get('shoppingCart');
  cart.addItem(item);
  Session.set('shoppingCart',cart);
  </code></pre>
  
  <p>Somehow, it does not work. When I take a look ad the chrome console it says <code>undefined is not a function</code>, pointing at <code>cart.addItem(item)</code> line. If I change it to this, it will work , but of course since everytime new shopping cart is created, I cannot accumulate items in the cart.</p>
  
  <pre><code>var cart = new ShoppingCart();
  cart.addItem(item);
  Session.set('shoppingCart',cart);
  </code></pre>
  
  <p>How should I store and retrieve the object from session properly? It looks like the returned object from the Session.get() somehow not considered as ShoppingCart. Did I miss any type cast?</p>