简体   繁体   English

等待功能,承诺结束

[英]Wait function with promise to end

I am trying to get a collection from firebase and later call a function that will insert data. 我试图从firebase获取一个集合,然后调用一个将插入数据的函数。 Problem is that the insert data is been called before the compare function ends... 问题是在比较函数结束之前调用了插入数据...

This is the guide that I am following: 这是我遵循的指南:

1 - Get the game inputs and create an array with the videos to recommend 1 - 获取游戏输入并创建一个包含要推荐的视频的阵列

2 - Get the already recommended videos from user 2 - 从用户那里获取已推荐的视频

3 - Compare the videos with the array to exclude duplicates. 3 - 将视频与阵列进行比较以排除重复项。

4 - Proceed to next function to insert the array to firebase. 4 - 继续执行下一个功能,将阵列插入firebase。

Problem is that for step 4 I need step 3 to be completed. 问题是,对于第4步,我需要完成第3步。 But that's not happening. 但那并没有发生。 Any suggestion on how to solve this? 关于如何解决这个问题的任何建议?

export const generateRecommendationsFromGame = (participantID) => {
  var newRec = [];
  let userRef = firebase.firestore().collection("users").doc(participantID);


  //1 Game inputs
  return firebase.firestore().collection("inputGame").where("user", "==", userRef).onSnapshot({}, function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      let consagua = doc.data().consagua;
      let enfart = doc.data().enfartamento;
      let mastig = doc.data().mastigacao;
      let mexdorm = doc.data().mexerdormir;
      let emoc = doc.data().emocoes;

      //13
      //V2.3.2
      if (Number(consagua) >= 3) {
        newRec.push('V2.3.2');
      }
      //15
      //V3.1
      if (Number(enfart) >= 25) {
        newRec.push('V3.1');
      }
      //16
      //V3.2
      if (Number(mastig) >= 40) {
        newRec.push('V3.2');
      }
      //17
      //V3.3
      //18
      //V3.4
      if (Number(mexdorm) >= 3) {
        newRec.push('V3.3');
        newRec.push('V3.4');
      }
      //19
      //V3.5
      if (Number(emoc) >= 2) {
        newRec.push('V3.5');
      }


      //2 - User Videos
      return firebase.firestore().collection("recommendedVideo").where("user", "==", userRef).get().then(result2 => {
        result2.forEach(doc2 => {

          //3 - Get video ref to compate to Array
          var videoFef = doc2.data().video.id;
          firebase.firestore().collection("videos").doc(videoFef).get().then(
            function(doc2) {
              if (doc2.exists) {
                var sequence = doc2.data().sequenceNumberID;
                for (var i = 0; i < newRec.length; i++) {
                  if (String(sequence) == String(newRec[i])) {
                    var index = newRec.indexOf(newRec[i]);
                    if (index > -1) {
                      newRec.splice(index, 1);
                    }
                  }
                }
              } else alert("Não existe doc videos");
            }
          )
        });
      }).then(() => {
         insertData(newRec);
      })
    });
  })
};

Then, after the video is compared and the duplicated excluded, I want to call a functions to insert the videos on Firebase. 然后,在比较视频并删除重复后,我想调用函数在Firebase上插入视频。

export const insertData= (arr) => {   
  var newRec = arr.split(',');
  for (var i = 0; i < newRec.length; i++) {
    var ref = newRec[i];
    firebase.firestore().collection("videos").where("sequenceNumberID", "==", ref.toString()).onSnapshot(function (querySnapshot) {
      querySnapshot.forEach(function (doc) {
        firebase.firestore().collection("recommendedVideo").add({
          seen: false,
          user: firebase.firestore().collection('users').doc(participantID),
          video: firebase.firestore().collection('videos').doc(doc.id)
        }).catch(function (error) {
          alert(error);
        })
      });
    });
  }
}

You need to make an asynchronous query for each doc2 and then continue on to insertData after all queries are done. 您需要为每个doc2创建一个异步查询,然后在完成所有查询后继续insertData The right tool for this is Promise.all , which takes an array of promises and resolves once all promises in the array are resolved. 正确的工具是Promise.all ,它会获取一系列的promise并在解析数组中的所有promise后解析。 So, map each doc2 to a Promise. 因此,将每个doc2映射到Promise。

In addition to Promise.all , use async / await to greatly reduce your indentation hell. 除了Promise.all ,使用async / await可以大大减少你的缩进地狱。

Note that you're shadowing (duplicating) the doc2 variable inside the forEach , which can easily cause confusion - better to call it some other name (eg doc3 ): 请注意,您在forEach隐藏(复制) doc2变量,这很容易引起混淆 - 最好将其称为其他名称(例如doc3 ):

Make the querySnapshot.forEach async: 使querySnapshot.forEach异步:

querySnapshot.forEach(async (doc) => {

and then: 然后:

//2 - User Videos
const result2 = await firebase.firestore().collection("recommendedVideo").where("user", "==", userRef).get();
await Promise.all(result2.map(async (doc2) => {
  //3 - Get video ref to compate to Array
  const videoFef = doc2.data().video.id;
  const doc3 = await firebase.firestore().collection("videos").doc(videoFef).get();
  if (!doc3.exists) {
    alert("Não existe doc videos");
    return;
  }
  var sequence = doc3.data().sequenceNumberID;
  for (var i = 0; i < newRec.length; i++) {
    if (String(sequence) !== String(newRec[i])) return;
    var index = newRec.indexOf(newRec[i]);
    if (index > -1) newRec.splice(index, 1);
  }
}));
// end of Promise.all
insertData(newRec);

Here is how I managed to solve this. 以下是我设法解决这个问题的方法。

        var reads = [];

        await firebase.firestore().collection("recommendedVideo").where("user", "==", userRef).onSnapshot({}, function (querySnapshot) {
            querySnapshot.forEach(async doc => {
                reads.push(doc);
            })
            return Promise.all(reads.map(async (doc1) => {
                //3 - Get video ref to compate to Array
                const videoFef = doc1.data().video.id;
                const doc3 = await firebase.firestore().collection("videos").doc(videoFef).get();
                if (!doc3.exists) {
                    alert("Não exisste doc videos");
                    return;
                }

                var sequence = doc3.data().sequenceNumberID;
                for (var i = 0; i < newRec.length; i++) {
                    if (String(sequence) == String(newRec[i])) {
                        var index = newRec.indexOf(newRec[i]);
                        if (index > -1) {
                            newRec.splice(index, 1);
                        }
                    }
                }
            })).then(function() {
                insertData(newRec, participantID);
            });
        });

Thanks CertainPerformance for the help. 感谢CertainPerformance的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM