问题
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