0

Updating the session object with a new value is not working in sails.

showBrowsePage: function(req, res) {

// If not logged in set `me` property to `null` and pass tutorials to the view
if (!req.session.userId) {
  return res.view('browse-tutorials-list', {
    me: null
  });
}

User.findOne(req.session.userId, function(err, user) {
  if (err) {
    return res.negotiate(err);
  }

  if (!user) {
    sails.log.verbose('Session refers to a user who no longer exists- did you delete a user, then try to refresh the page with an open tab logged-in as that user?');
    return res.view('homepage', {
      me: null
    });
  }

  req.session.me = "A test value";


  return res.view('browse-tutorials-list', {
    me: {
      email: user.email,
      gravatarURL: user.gravatarURL,
      username: user.username,
      admin: user.admin
    },
    showAddTutorialButton: true
  });
});

},

Here in this function call, trying to add a new value to session object "req.session.me" but it is not getting saved in the session object.

The res is also getting send after setting the value but still it does not reflect.

May13ank
  • 548
  • 2
  • 9
  • 24
  • Just for clarity, are you saying after you set req.session.me = "A test value"; if you were to console.log(req.session.me) nothing is stored? – Glen Jan 07 '18 at 17:00
  • Session is not updated. If I make a new request after completion of this request, the req.session.me is not available – May13ank Jan 07 '18 at 18:34
  • So you set req.session.me = "A test value"; then respond with the browse-tutorials-view and then on the next request back to the server, req.session.me is now undefined, is that correct? – Glen Jan 07 '18 at 19:12
  • Yes. That is correct. – May13ank Jan 08 '18 at 10:00
  • How/where are you setting req.session.userId? There doesn't appear to be much wrong with your code. Is the whole session lost or just the value you are storing in req.session.me? The only time I've experienced whole sessions dropping in sails is when moving from www to non www URLs or vice versa. If the whole session is not being dropped and instead just the value "A test value" you could try using req.session.save(); after setting the session value. You also might check to ensure that you are not overwriting req.session.me somewhere else; in a policy, in middleware or in another controller. – Glen Jan 08 '18 at 23:02
  • The whole session is not getting lost. It's just the value I am storing in req.session.me. The save() I have tried in this scenario and it works fine. But my point being, save should not be required at all. – May13ank Jan 10 '18 at 09:41
  • What version of sails are you using, I remember there being a similar bug with an earlier release. You could try opening a bug with the sails team in github if you are using v0.12* or v1. – Glen Jan 10 '18 at 10:02
  • v0.12 version of sails. – May13ank Jan 11 '18 at 05:42

0 Answers0