最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - change and refresh session data in Node.js and express-session - Stack Overflow

programmeradmin0浏览0评论

I'm using Node.js along with express-session and I've got this session data that holds the user information

req.session.user

now when the user updates the information, I set the session to the new data and then reload it

req.session.reload(function(err) {
req.session.user = user;
});

it should work but the req.session.user still shows the old information, why? :(
many thanks in advance

I'm using Node.js along with express-session and I've got this session data that holds the user information

req.session.user

now when the user updates the information, I set the session to the new data and then reload it

req.session.reload(function(err) {
req.session.user = user;
});

it should work but the req.session.user still shows the old information, why? :(
many thanks in advance

Share Improve this question asked Feb 13, 2015 at 15:51 Mohsen ShakibaMohsen Shakiba 1,8724 gold badges25 silver badges43 bronze badges 2
  • You are doing req.session.save() and that returns before you req.session.reload()? – Christopher Hackett Commented Feb 13, 2015 at 20:09
  • no I'm not doing any req.session.save() in my app, but it's so weird cause according to API the reload should do the trick, right? – Mohsen Shakiba Commented Feb 13, 2015 at 20:20
Add a comment  | 

1 Answer 1

Reset to default 16

You are reloading before saving your change. Saving usually happens at the end of the request automatically. There is a test in session.js which demonstrates that behaviour.

Using req.session.reload() reverts the changes that currently have been been made while processing the request ...

req.session.example = 'set on set_reload'
req.session.reload( function (err) {
    res.render('index', { title: req.session.example });
});
// next http get request
// req.session.example == value before the call above 

Consider using the session like this

req.session.example = 'set on set';
res.render('index', { title: req.session.example });
// next http get
// req.session.example == 'set on set'

If you have a real reason for using req.session.reload() then this type of usage will give you the desired behaviour

req.session.example = 'set on set_save_reload';
req.session.save( function(err) {
    req.session.reload( function (err) {
         res.render('index', { title: req.session.example });
    });
});
// next http get
// req.session.example == 'set on set_save_reload'
发布评论

评论列表(0)

  1. 暂无评论