$ q.all解析后对对象数组进行排序

时间:2017-02-14 16:13:50

标签: javascript angularjs sorting angular-promise

使用AngularJs编写应用程序。我想检索用户已加入的所有组的列表。

我写了以下内容:

controller('DashCtrl', function($scope, $q, $localStorage, AuthService, DatabaseService, GroupService) {
  console.log("*** DashCtrl ***");
  var user = AuthService.currentUser;
  $scope.user = user;
  var uid = $scope.user.auth.uid;

  $scope.joinedGroups = [];

  var tmpGroups = [];
  var addGroupDetailsPromises = [];

  GroupService.getUserJoinedGroupsArray($scope.user.auth.uid)
  .then(function(groups) {
    for (var i = 0; i < groups.length; i++) {
      var tmpGroup = {};
      tmpGroup.id = groups[i];
      addGroupDetailsPromises.push(addGroupDetails(tmpGroup));
      tmpGroups.push(tmpGroup);
    }
  })
  .catch(function(error) {
    console.log('Error retrieving groups: ', error);
  });

  // Once all details have been added to groups, sort groups
  $q.all(addGroupDetailsPromises).then(function() {
    console.log(tmpGroups.length);
    tmpGroups.sort(function(a, b) {
      console.log('a: ', a.details.timestamp);
      console.log('b: ', b.details.timestamp);
      return a.details.timestamp - b.details.timestamp;
    });

    console.log(tmpGroups);
    $scope.joinedGroups = tmpGroups;
  }).catch(function(error) { console.log(error); });

  function addGroupDetails(tmpGroup) {
    var promise = new Promise(function(resolve, reject) {
      GroupService.getGroupDetailsObj(tmpGroup.id).then(function(details) {
        tmpGroup.details = details;
        tmpGroup.display = {};
        var eventDate = new Date(details.timestamp);
        var today = new Date();

        if (eventDate >= today) {
          tmpGroup.display.day = eventDate.getDate();
          tmpGroup.display.month = monthNames[eventDate.getMonth()].substr(0, 3);
          tmpGroup.display.time = eventDate.getHours() + ":" + eventDate.getMinutes();

        }

        resolve();
      });
    });

    return promise;
  };

  var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

})

我的数组(tmpGroups)永远不会被排序。根据我的console.log(tmpGroups.length)的输出,数组的长度等于0 ...我猜这就是它永远不会被排序的原因。

但我不明白结果,因为我的数组应该填充所有信息,因为我使用$ q.all等待所有承诺得到解决。

有什么建议吗?

由于

1 个答案:

答案 0 :(得分:0)

这是因为GroupService.getUserJoinedGroupsArray是异步调用。你必须等到它完成然后将列表推送到tmpGroups然后执行promise。尝试将承诺置于其中

GroupService.getUserJoinedGroupsArray($scope.user.auth.uid)
  .then(function(groups) {
    for (var i = 0; i < groups.length; i++) {
      var tmpGroup = {};
      tmpGroup.id = groups[i];
      addGroupDetailsPromises.push(addGroupDetails(tmpGroup));
      tmpGroups.push(tmpGroup);
    }

    // Once all details have been added to groups, sort groups
    $q.all(addGroupDetailsPromises).then(function() {
       console.log(tmpGroups.length);
       tmpGroups.sort(function(a, b) {
         console.log('a: ', a.details.timestamp);
         console.log('b: ', b.details.timestamp);
         return a.details.timestamp - b.details.timestamp;
    });

      console.log(tmpGroups);
     $scope.joinedGroups = tmpGroups;
    }).catch(function(error) { console.log(error); });
    })
  .catch(function(error) {
    console.log('Error retrieving groups: ', error);
  });