0

我有以下代码,我试图将一些数据存储到会话变量中:

//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 的调用中获取会话数据。

任何想法:谢谢

4

1 回答 1

0

我发现了问题。next() 回调需要在 ip2Location 调用中。

于 2012-11-15T19:39:10.383 回答