Reputation: 27
I have express session, which initializing in route method:
router.post('/add', function (req, res) {
if (!req.session.cart) {
req.session.cart = cartController.initializeCart();
}
cartController.addToCart(req.body.productId, req.session.cart, function(error, result) {
error ? res.send(error) : res.send(200);
})
});
And I have cart model as function
function cart() {
this.ids = [];
this.addProduct = function (id) {
ids.push(id);
}
}
Initialize method:
var cartModel = require("../models/cart"); //UPD
initializeCart: function() {
return new cartModel(); //UPD exception throw right after this operation
},
Add cart method:
addToCart: function(id, cart, callback) {
cart.addProduct(id);
.....
Session:
var expressSession = require('express-session');
app.use(expressSession({
secret: "someSecret"
}));
But calling cart.addProduct() doesn't occurs, because addProduct() is undefined. Using prototype doesn't help me.
Screen: http://prntscr.com/5rz61t
Upvotes: 0
Views: 83
Reputation: 5131
If cart
is saved in a session storage system that does not keep or restore types, cart
will not be a cart instance any longer but just a simple object.
router.post('/add', function (req, res) {
var cart;
cart = cartController.initializeCart();
if (req.session.cart) {
cart.ids = req.session.cart.ids;
} else {
req.session.cart = cart;
}
cartController.addToCart(req.body.productId, cart, function(error, result) {
error ? res.send(error) : res.send(200);
})
});
Upvotes: 1
Reputation: 11255
There is no cartModel
function in your code.
Try return new cart();
instead of return new cartModel();
Upvotes: 1