Cannot read property 'totalQty' of undefined

Cannot read property 'totalQty' of undefined



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.



Here is my Error


/home/ubuntu/workspace/views/partials/header.ejs:37
35| </li>
36| <li>
>> 37| <a href="cart" class="navport"><span
class="badge"><%= session.cart.totalQty || 0%></span></a>
38| </li>
39| </ul>
40|

Cannot read property 'totalQty' of undefined



App.js


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");




models/cart.js



This is the cart Object


module.exports = function Cart(oldCart) 0;
this.totalPrice = oldCart.totalPrice ;



header.ejs



This is the HTML where I'm trying to list the total amount in the cart on the navbar


<li>
<a href="cart" class="navport"><span class="badge"><%=
session.cart.totalQty || 0%></span></a>
</li>
</ul>




2 Answers
2


var Cart = require('../models/cart');



(path for cart model)
did you include this line in your route ? because of that you are getting cart it is undefined error. please make sure that you have included above line in your route file. when you create a cart object this line will help you refer the cart model.



I've faced the same issue while loading the index template. Can you check if session.cart is available before rendering shopping-cart quantity? I added simple if condition like below and it worked for me.


<% if(session.cart) %>
<span class="badge"><%= session.cart.totalQty %></span>
<% %>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ