我有以下代码,我试图将一些数据存储到会话变量中:
//Capture IP geo-location info if it hasn't been set for this session
app.all('*', function(req, res, next){
//Stuff to run once for each user session
if (!req.session.runOnce){
req.session.runOnce = true;
if (!req.session.ipinfo || 1){
//add user ip to sessions for use on who's online
//if we're localhost, give it an 8.8.8.8 IP just so we can test something
if (req.ip == '127.0.0.1'){ip = '8.8.8.8'; }else{ip = req.ip; }
ip2location(ip, config.ipinfodb.apiKey, function(error, data){
var ipData = JSON.parse(data);
req.session.ipData = ipData;
});
next();
}else{
next();
}
}else{
next();
}
});
session.ipData 没有被存储。我可以将它存储在对 ip2location 的调用之外,并且我可以在对 ip2location 的调用中获取会话数据。
任何想法:谢谢