title: Cannot read property 'totalQty' of undefined
body: <p>I've created a cart object, which can store the products in a cart. The problem is before an item is put into the cart it is undefined.  How do I make the cart defined even when nothing is in it during the session? I'm using ejs. </p>
  
  <p>Here is my Error </p>
  
  <pre><code>/home/ubuntu/workspace/views/partials/header.ejs:37
      35|                 &lt;/li&gt;
      36|                 &lt;li&gt;
   &gt;&gt; 37|                   &lt;a href="cart" class="navport"&gt;&lt;span 
  class="badge"&gt;&lt;%= session.cart.totalQty || 0%&gt;&lt;/span&gt;&lt;/a&gt;
      38|                 &lt;/li&gt;
      39|               &lt;/ul&gt;
      40|               
  
  Cannot read property 'totalQty' of undefined
  </code></pre>
  
  <h1>App.js</h1>
  
  <pre><code>var express = require("express");
  var app = express();
  var mongoose = require("mongoose"),
      User = require("./models/user"),
      bodyParser = require("body-parser"),
      ejs         = require("ejs"),
      passport = require("passport"),
      LocalStrategy = require("passport-local"),
      localMongoose = require("passport-local-mongoose"),
      session = require("express-session"), 
      MongoStore = require("connect-mongo")(session),
      Cart = require("./models/cart"),
      Clothes = require("./models/clothes");
  mongoose.connect("mongodb://localhost/bitchinvintage");
  app.set("view engine", "ejs");
  app.use(express.static(__dirname + "/public"));
  
  
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(session({
      secret: "random dogga",
      resave: false,
      saveUninitialized: false,
      store: new MongoStore({mongooseConnection: mongoose.connection}),
      cookie: {maxAge: 180 * 60 * 1000}
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  
  passport.use(new LocalStrategy(User.authenticate()));
  passport.serializeUser(User.serializeUser());
  passport.deserializeUser(User.deserializeUser());
  app.use(function (req, res, next) {
    res.locals.login = req.isAuthenticated();
    res.locals.session = req.session;
    next();
  });
  app.get("/", function(req, res){
      res.render("landing");
  });
  // Clothes Routes
  app.get("/clothes", function(req, res){
      // Get all campgrounds from DB
      Clothes.find({}, function(err, clothes){
         if(err){
             console.log(err);
         } else {
            res.render("clothes",{clothes:clothes});
         }
      });
  });
  app.get("/clothes/new", function(req, res){
      res.render("new");
  });
  
  app.post("/clothes", function(req, res){
      var name = req.body.name;
      var overlay = req.body.overlay;
      var price = req.body.price;
      var image = req.body.image;
      var newClothes = {name: name, image: image, price: price, overlay: overlay}
      Clothes.create(newClothes, function(err, newlyCreated){
          if(err){
              console.log(err);
          } else{
              res.redirect("/clothes");
          }
      });
  });
  // ------------------
  
  
  // Register Routes
  app.get("/register", function(req, res){
      res.render("register");
  });
  app.post("/register", function(req, res){
      var newUser = new User({username: req.body.username});
      User.register(newUser, req.body.password, function(err, user){
          if (err){
              console.log(err);
              return res.render("register");
          }
          passport.authenticate("local")(req, res, function(){
              res.redirect("/");
          })
      })
  });
  // -----------
  
  // Login Routes
  app.post("/login", passport.authenticate("local", {
      successRedirect: "/",
      failureRedirect: "/register"
  }), function(req, req){
  
  });
  
  // Cart Routes
  app.get("/add-to-cart/:id", function(req, res, next){
      var productId = req.params.id;
      var cart = new Cart(req.session.cart ? req.session.cart : {});
      console.log("works");
      Clothes.findById(productId, function(err, clothes){
          if(err){
              return res.redirect("/");
          }
          cart.add(clothes, productId);
          req.session.cart = cart;
          console.log(req.session.cart);
          res.redirect("/clothes");
      });
  });
  // ------------
  app.get("/account", isLoggedIn, function(req, res){
  });
  app.get("/*", function(req, res){
      res.render("error");
  });
  
  app.listen(process.env.PORT, process.env.IP, function(){
      console.log("Vintage server Starting...")
  });
  
  function isLoggedIn(req, res, next) {
      if (req.user) {
          next();
      } else {
         res.redirect("/account"); 
      }
  }    
  </code></pre>
  
  <h1>models/cart.js</h1>
  
  <p>This is the cart Object</p>
  
  <pre><code>module.exports = function Cart(oldCart) {
      this.items = oldCart.items || {};
      this.totalQty = oldCart.totalQty || 0;
      this.totalPrice = oldCart.totalPrice || 0;
  
  
      this.add = function(item, id) {
          var storedItem = this.items[id];
          if (!storedItem){
              storedItem = this.items[id] = {item: item, qty: 0, price: 0}
          }
          storedItem.qty++;
          storedItem.price = storedItem.item.price * storedItem.qty;
          this.totalQty++;
          this.totalPrice += storedItem.price;
      }
      this.generateArray = function() {
          var arr = [];
          for (var id in this.items) {
              arr.push(this.items[id]);
  
          }
          return arr;
      }
  };
  </code></pre>
  
  <p><strong>header.ejs</strong> </p>
  
  <p>This is the HTML where I'm trying to list the total amount in the cart on the navbar </p>
  
  <pre><code>&lt;li&gt;
  &lt;a href="cart" class="navport"&gt;&lt;span class="badge"&gt;&lt;%= 
  session.cart.totalQty || 0%&gt;&lt;/span&gt;&lt;/a&gt;
  &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>