试图了解节点js和猫鼬中的异步等待

时间:2020-07-20 03:17:29

标签: node.js mongoose async-await

有人可以帮我这个忙吗?我有一个for循环,它正在数组上进行迭代。在for循环的中间,我想将for循环中的值传递给猫鼬查询,并在集合中进行一些更改。然后,一旦查询成功执行,我想继续进行数组的下一次迭代。我已经写了这样的代码。如果这完全是垃圾,请原谅我。目前,我的异步函数中绝对没有任何执行。函数外部的所有内容都会执行。所以基本上不能按预期工作。有人可以帮忙吗?我绝对不熟悉javascript,mongo和node,只是想更好地理解这些概念。

for(i=0;i<dataarray.length;i++){
                console.log("Hi i just entered the for loop")
                if((slot1counter > 4) && (slot2counter > 4) && (slot3counter > 4) && (slot4counter > 4)
                  && (slot5counter > 3) && (slot6counter > 4) && (slot7counter > 4) && (slot8counter > 4)
                  && (slot9counter > 4) && (slot10counter > 4) && (slot11counter > 3) && (slot12counter > 4)) {
                    unusedarray.push(dataarray[i]);
                    continue;
                  } else {
                    if(dataarray[i].Matched_Slots.length !== 0){
                      console.log("hi i am just about to enter asynch await")
                      async function checkPlayer()
                      {
                        try {
                        const doc = await GSM.findOne({Player_Name: dataarray[i].Player1_Name}).exec()
                        console.log("I am inside async and await")
                        console.log(dataarray[i].Player1_Name)
                        console.log(doc);
                      } catch (err) {
                        console.log(err)
                      }
                        //console.log(player.Player_Name)
                      }
                      console.log("I just left asynch await")

0 个答案:

没有答案