数据的顺序不是正确的nodejs

时间:2016-04-21 08:52:43

标签: jquery node.js mongodb

我对nodejs有点新意。我正在建立一个像应用程序的Facebook。无论如何,我正在查询代码中显示的数据库。但是前端的记录I console.log的顺序是混乱的而且不合适,每次都不同。你能告诉我我在哪里错了吗?

router.get('/home', isAuthenticated, function(req, res){
    var friendsIds = req.user.friends;
    friendsIds.push(req.user.id);
    var cbdone = 0;

    Status.find({statuserId : {$in: friendsIds}}, null, {skip : 0, limit: 5, sort: {_id: -1}}, function(err, statuses) {

        for (var i = 0; i < statuses.length; i++) {
            (function(currStatus){
                var currentStatus = currStatus;
                var a = currentStatus._id.getTimestamp();
                if(currentStatus.likedBy){
                    if (currentStatus.likedBy.indexOf(req.user.id) > -1) {
                        currentStatus.isLiked = true;
                    };
                    currentStatus.likersCount = currentStatus.likedBy.length;
                } else {
                    currentStatus.isLiked = false;
                    currentStatus.likersCount = 0;
                }

                Comment.find({statusId : currentStatus.id}, null, {sort: {_id: -1}}, function(err, comments){
                    for (var i = comments.length - 1; i >= 0; i--) {
                        comments[i].likersCount = comments[i].likedBy.length;
                        comments[i].isLiked = comments[i].likedBy.indexOf(req.user.id) > -1;
                    };
                    currentStatus.comments = comments;
                    cbdone++;
                    if(cbdone === statuses.length){
                        res.render('home2', {
                            user: req.user, 
                            statuses: statuses
                        }); 
                    }
                });

            })(statuses[i]);            
        };
    });
});

1 个答案:

答案 0 :(得分:0)

您应该使用Promise.all或Promise + co或async / await来进行异步调用&#39;结果有序。

Status.find({statuserId : {$in: friendsIds}}, null, {skip : 0, limit: 5, sort: {_id: -1}}, function (err, statuses) {
  co(function *() {
    const Comments = yield statuses.map(function (currentStatus) {
      var currentStatus = currStatus;
      var a = currentStatus._id.getTimestamp();
      if(currentStatus.likedBy){
        if (currentStatus.likedBy.indexOf(req.user.id) > -1) {
          currentStatus.isLiked = true;
        };
        currentStatus.likersCount = currentStatus.likedBy.length;
      } else {
        currentStatus.isLiked = false;
        currentStatus.likersCount = 0;
      }
      return new Promise(function (resolve, reject) {
        Comment.find({statusId : currentStatus.id}, null, {sort: {_id: -1}}, function(err, comments){
          if(err) return reject(err);
          for (var i = comments.length - 1; i >= 0; i--) {
            comments[i].likersCount = comments[i].likedBy.length;
            comments[i].isLiked = comments[i].likedBy.indexOf(req.user.id) > -1;
          };
          resolve(comments);
        });
      })
    });
 ......
    res.render('home2', {
      user: req.user,
      statuses: statuses
    });
  })

});