[英]change and refresh session data in Node.js and express-session
I'm using Node.js along with express-session and I've got this session data that holds the user information 我正在将Node.js与Express-Session一起使用,并且此会话数据保存了用户信息
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? 它应该可以工作,但是req.session.user仍然显示旧信息,为什么? :(
:(
many thanks in advance 提前谢谢了
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 .
session.js中有一个测试可以证明这种行为 。
Using req.session.reload()
reverts the changes that currently have been been made while processing the request ... 使用
req.session.reload()
还原在处理请求时当前已进行的更改...
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.reload()
则这种用法将为您提供所需的行为
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.