Sails JS - Sessions

Every time the page reloads, I run this function:

init: function(req, res) { console.log("Session: "); console.log(req.session); if (req.session.player_id !== undefined) { Player.getPlayer(req.session.player_id); } else { req.session.player_id = sails.uuid.v4(); req.session.save(); console.log("Session saved"); Player.createPlayer(req.session.player_id); console.log("Creating player: " + req.session.player_id); } } 

The problem is that every time I run

  console.log("Session: "); console.log(req.session); 

Player_id does not exist, and every time the page reloads, the if statement returns false.

 console.log("Creating player: " + req.session.player_id); 

It returns the correct value, but I cannot understand why the session does not follow the user every time the page is reloaded?

+5
source share
1 answer

It seems like the solution was so simple:

 res.send(req.session); 

If I add this to the end of the else statement, the session will be saved and can be reused.

Why so, I do not know. If someone wants to figure it out, be my guest.

+6
source

All Articles