jeudi 18 novembre 2021

Session variable not storing data in NodeJs and ExpressJS

I'm trying to store user ID in user session in Express but for some reason the session forgets the value after the program exits the API path in which it was assigned. Here's the session setup in index.js:

var session = require("express-session");

app.use(
session({
secret: "abcdefghijklmnopqrstuvwxyz",
resave: false,
saveUninitialized: false,
 }) 
);

Here's where i'm setting the session variable:

req.session._id = user._id;
console.log("Request session inside agent login : " + req.session._id);//Prints the correct value like it's supposed to

And here's where I want to access the said variable but it outputs undefined:

console.log("Agent id inside Brand : " + req.session._id); //Prints undefined

I've been frustrated with this for a while now. If anyone can help me with this it would be very appreciated Thanks




Aucun commentaire:

Enregistrer un commentaire