【问题标题】:Node.js / Expressjs do not clear sessionNode.js / Expressjs 不清除会话
【发布时间】:2015-01-10 18:42:45
【问题描述】:

应用程序在开始时通过 Angularjs / $http.get() 加载一些用户,向下滚动时加载更多(无限滚动)。

该页面还有一些位置过滤器,因此用户可以选择不同的国家和城市来仅查看来自该位置的用户。

控制器是这样的:

$scope.country;
$scope.city;
$scope.users = [];
$scope.busy = false;
$scope.destroy = null;

$scope.nextPage = function() {
  if ($scope.busy) return;
  $scope.busy = true;

  $http.get('/users', {
    params: {
      destroy: $scope.destroy,
      country: $scope.country,
      city: $scope.city
    }
  }).success(function(res) {
    var users = res.users;
    for (var i = 0; i < users.length; i++) {
      $scope.users.push(users[i]);
    }
    $scope.busy = false;
    $scope.destroy = null;
  }).error(function() {
    alertify.error('Error loading Escorts');
  });
};
  • 在位置更改时,我调用返回新国家和城市的服务;
  • 所以我设置了这些信息并清空了 users 数组;
  • 我还设置了 $scope.destroy 来告诉服务器清除会话;
  • 在会话中,我存储页面中已加载的用户的 ID 以避免重复。

这是在同一个控制器中处理位置变化的代码:

$scope.$on('handleLocationBroadcast', function() {
  $scope.country = SharedLocationService.country;
  $scope.city = SharedLocationService.city;
  $scope.destroy = 'destroy';
  $scope.users = [];
  $scope.nextPage();
});

服务器端我使用的是 Expressjs / Node 并且控制器非常简单:

exports.getUsers = function(req, res) {
    // If parameter destroy is 'destroy' then clear the session.skip.
    if (req.query.destroy === 'destroy') req.session.skip = [];

    // set skip with session or empty array (need for the first call)
    var skip = req.session.skip || [];

    // set locations (I use to store also the location in a session for future calls
    var country = req.query.country || req.session.country || { $ne: '' };
    var city = req.query.city || req.session.city || { $ne: '' };

    // Get users
    User.find({ '_id' : { '$nin': skip }})
    .where( { 'location.country' : country, 'location.city' : city } )
    .sort({ _id: -1 })
    .limit(15)
    .exec(function(err, users) {

    if (err) res.json({ 'msg': 'Error loading users' });

    for (var i = 0; i < users.length; i++) {
        skip.push(users[i]._id);
    }

    req.session.skip = skip;

    res.json({
      users: users
    });

  });
};

在我更改位置之前,一切似乎都非常顺利。

当我更改位置时,似乎会话已正确清除,但当我向下滚动呼叫其他用户时,我发现会话尚未清除,导致用户加载严重不足。

因此,如果我正在浏览来自英国所有城市的用户,并且我选择仅在更改位置时切换到伦敦,它会清空用户数组,正确加载 15 个用户,但向下滚动时它不会再加载少数用户出现在上一个会话中,并查看我收到的整个列表的会话日志。

我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: javascript angularjs node.js session express


    【解决方案1】:

    好吧,我自己解决了这个问题,通过POST 请求清除更改位置上的会话,然后再次调用$scope.nextPage();

    所以基本上代码看起来像:

    $scope.$on('handleLocationBroadcast', function() {
      $scope.country = SharedLocationService.country;
      $scope.city = SharedLocationService.city;
      $scope.users = [];
    
      $http({
        url: '/destroy',
        method: 'POST',
        data: { destroy: true }
      }).then(function(res) {
        $scope.nextPage();
      }, function() {
        alertify.error('Error loading users');
      });
    }
    

    对服务器的另一个请求,是的,但它现在解决了问题。如果有更好的方法,我会很高兴看到它并学习新的东西。

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 2012-04-26
      • 2014-08-12
      • 2012-07-19
      • 1970-01-01
      • 2010-12-05
      • 2015-04-16
      • 2011-10-02
      相关资源
      最近更新 更多