Sails.js session undefined on page refresh

纵然是瞬间 提交于 2019-12-12 05:28:48

问题


In my sails.js application i am setting up session like this

    req.session.userId="hello";
    console.log(req.session.userId);

after the first request i hide this line req.session.userId="hello";

after that console.log(req.session.userId) returns undefined


回答1:


Tough without seeing more of your controller code, but the session property you are setting is only going to be processed into session upon completing the controller action - e.g. returning a view or json.

Try:

if(req.session && req.session.userId){
  //This user has an active session already so we have a userid
  sails.log.info(req.session.userId);
else{
  //Must be the first time this action was executed
  req.session.userId = 'hello';
}
res.view('index');

where index is the name of your ejs view. If you never return anything then the session property is never set.



来源:https://stackoverflow.com/questions/44541025/sails-js-session-undefined-on-page-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!