I have a route written in Express as the following:
app.get("/route", function(req, res){
if (someBoolean){
someFunction(stuff, function(user){
...
req.session.user = user;
});
}
res.render("route");
}
When someBoolean is true, the session should be set. However, after moving on to a different page, req.session.user comes back as undefined. Checking req.session.user inside someFunction comes back defined however. A solution I have found to the session becoming undefined outside the scope of someFunction was to move res.render("route") into someFunction as such:
app.get("/route", function(req, res){
if (someBoolean){
someFunction(stuff, function(user){
...
req.session.user = user;
res.render("route");
});
}
}
This however causes the route to fail when someBoolean is false. I'm thinking this is caused by the asynchronous nature of Node.js, but I am not sure. What is causing this to occur exactly, and is this the proper solution?