I am new to nodejs/express and currently trying to figure out how to store objects of a certain class in session storage (which I understand is basically a serialized version of the req.session
object).
My (shortened example is this):
//cart.js
const CartItem = require('./cartItem');
class Cart {
_items = [];
constructor() {
// ...
}
// ...
addItem(item) {
// ...
}
}
module.exports = Cart;
//routes.js
router.get('/create', function (req, res, next) {
req.session.cart = new Cart();
req.session.cart.addItem(new CartItem('Red Sox', 15, Math.round(Math.random() * 5)));
console.log(req.session);
res.render('cart/create.twig', {
cart: req.session.cart
});
});
router.get('/overview', function (req, res, next) {
console.log(req.session);
res.render('cart/overview.twig', {
cart: req.session.cart
});
});
The problem I am facing is that during the first action (router.get('/create'
) the req.session.cart
property holds an object of the Cart
class.
However, when loading from the session in the second action (router.get('/overview'
) the req.session.cart
property holds an object of no special class.
I assume/understand that JavaScript cannot know during deserialization what kind of object this JSON once has been. But how do other people solve this issue ?
I have been checking out other session middlewares (I currently use express-session), but none of them mention in detail, whether they are able to do what I am looking for.
Please help me understand and resolve this issue :)
Thanks everyone!