For循环不从猫鼬回调中更新数组

时间:2019-02-28 05:21:25

标签: javascript mongoose

我认为我的问题在于JS的异步特性。我正在尝试将项目推入数组中,但似乎并未对其进行更新...我在for循环中做了console.log语句,看到它用数字填充了数组,但是当我console.log循环外的数组,我得到一个空数组。我正在用猫鼬。 有什么建议吗?

代码如下:

let collections = [];
                return Promise.all(courts.map(court => {
                  return new Promise((resolve, reject) => {
                    return Promise.all(court.users.map(async user => {
                      let tempPromise = new Promise((resolve, reject) => { 
                          setTimeout(() => {
                              resolve();
                          }, 5000);
                      });
                                  return SignDetail.find({
                                    userName: user.userName,
                                    signStatus: "signIn",
                                  }).then(function(sign) { 
                                    if (user.userName.endsWith('zs')) { 
                                        let signCount = 0;
                                        if (sign.length > 1) {
                                            for (let j = 0; j < sign.length; j++) {
                                                let courtObj = {courtName: sign[j].userName}; //make court object
                                                signCount++; //increment each time there's a signature
                                                if (j === sign.length - 1) { //only push object in array when all signatures have been counted
                                                    courtObj.signCount = signCount;
                                                    collections.push(courtObj);
                                                    console.log(collections)
                                                } 
                                            }
                                        } 

                                    } //end here
                                  }); 
                      return tempPromise;
                    })).then(_ => resolve(collections));
                  })
                })).then(collections => {
                  // HERE you will your collection and you can use this promise where this function is being called. 
                  console.log(collections);
                });

1 个答案:

答案 0 :(得分:0)

函数SignDetail.find()是异步函数,您无法同步返回res.render。您需要从此函数返回一个承诺,以解决所需的输出。

您可以执行以下操作。

let collections = [];
return Promise.all(courts.map(court => {
  return new Promise((resolve, reject) => {
    return Promise.all(court.users.map(oUser => {
      var tempPromise = new Promise();
      if(oUser.userName.endsWith('zs')){
        SignDetail.find(
          {username: oUser.username. signStatus: 'signIn'}, 
          function(err, sign){
            collection.push(sign.length);
            tempPromise.resolve();
          })
      } else{
         tempPromise.resolve();
      }
      return tempPromise;
    })).then(_ => resolve());
  })
})).then(_ => {
  // HERE you will your collection and you can use this promise where this function is being called. 
  console.log(collections);
});