Express session not persisting
express, javascript, node.js
Solution
As it turns out, the issue wasn't with Express' session (as the other answers seem to think). Rather, it was a misunderstanding on my part. I changed `addClassToCart` to the following:
addClassToCart: function(req, res) {
req.session.cart = req.body.classId;
console.log(req.session.cart);
res.send('class added');
}
Adding `res.send()` fixed the problem.
Problem
I'm trying to set up a basic session system in node. Here's what I've got so far: app.js: ``` app.use(express.cookieParser('stackoverflow')); app.use(express.session()); ``` I'm setting the session data in ajax.js: ``` addClassToCart: function(req, res) { req.session.cart = req.body.classId; console.log(req.session.cart); } ``` This logs the correct information. However, when I try to retrieve that information elsewhere (same file, different function): ``` console.log(req.session.cart); ``` I get `undefined`. I feel like I'm missing something incredibly basic. Various tutorials for this are either awful or require me to add in even more packages (something I'm trying to avoid). More data from my debugging: - This works with non-AJAX requests - The session is set before it's logged.