【问题标题】:Firebase Firestore - Compare data from 2 collectionsFirebase Firestore - 比较来自 2 个集合的数据
【发布时间】:2021-04-28 06:41:04
【问题描述】:

我的 firebase 数据库有问题。我希望能够检索和比较 2 个不同集合的内容。在这种情况下,我想检查用户的答案是否与另一个集合中的答案键的答案相同。问题是我认为查询是异步完成的,所以我无法将值保存在 docRef.get() 函数之外。提前感谢您的帮助。

下面是我得到用户答案的​​地方:

firebase.auth().onAuthStateChanged((user) => {
         if (user) {
             var uid = user.uid;
             document.getElementById("useruser").innerHTML = user.displayName;

              var docRef = db.collection("Quiz").doc(user.displayName);
              docRef.get().then(function(doc) {
                    if (doc.exists) {
                       console.log("Document data:", doc.data());
                       document.getElementById("question1_1").innerHTML = doc.data().Question; //Here I have my user's answer
                       document.getElementById("reponse1_1_1").innerHTML = doc.data().Reponse1;
                       var lisetRep = doc.data().Reponse1;
                       document.getElementById("reponse1_1_2").innerHTML = doc.data().Reponse2;
                       document.getElementById("reponse1_1_3").innerHTML = doc.data().Reponse3;
                       document.getElementById("reponse1_1_4").innerHTML = doc.data().Reponse4;
                    } else {
                       // doc.data() will be undefined in this case
                       console.log("No such document!");
                    }
              }).catch(function(error) {
                    console.log("Error getting document:", error);
              });



        } else {
           window.location.href="account-connexion.html";
        }
     });

下面是我得到正确答案的地方

var docRef = db.collection("ReponseJ").doc("ReponseJuste");
                  docRef.get().then(function(doc) {
                        if (doc.exists) {
                           console.log("Document data:", doc.data());
                           document.getElementById("question1_1").innerHTML = doc.data().Question; //Here I have the corrected answer
                           document.getElementById("reponse1_1_1").innerHTML = doc.data().Reponse1;
                           var lisetRep = doc.data().Reponse1;
                           document.getElementById("reponse1_1_2").innerHTML = doc.data().Reponse2;
                           document.getElementById("reponse1_1_3").innerHTML = doc.data().Reponse3;
                           document.getElementById("reponse1_1_4").innerHTML = doc.data().Reponse4;
                        } else {
                           // doc.data() will be undefined in this case
                           console.log("No such document!");
                        }
                  }).catch(function(error) {
                        console.log("Error getting document:", error);
                  });

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您需要chain the promises,并使在第一个then() 块中捕获的值在第二个then() 块中可用,如下所示。我使用了一个数组,但您可以使用任何其他等效方法。

          var answerArray = [];
    
          docRef
            .get()
            .then(function (doc) {
              if (doc.exists) {
                answerArray.push(doc.data().Reponse1);
                answerArray.push(doc.data().Reponse2);
                answerArray.push(doc.data().Reponse3);
                answerArray.push(doc.data().Reponse4);
    
                console.log('Document data:', doc.data());
                document.getElementById(
                  'question1_1'
                ).innerHTML = doc.data().Question; //Here I have my user's answer
                document.getElementById(
                  'reponse1_1_1'
                ).innerHTML = doc.data().Reponse1;
                var lisetRep = doc.data().Reponse1;
                document.getElementById(
                  'reponse1_1_2'
                ).innerHTML = doc.data().Reponse2;
                document.getElementById(
                  'reponse1_1_3'
                ).innerHTML = doc.data().Reponse3;
                document.getElementById(
                  'reponse1_1_4'
                ).innerHTML = doc.data().Reponse4;
    
                // See the return below
                return db.collection("ReponseJ").doc("ReponseJuste").get(); 
    
              } else {
                // doc.data() will be undefined in this case
                console.log('No such document!');
                throw new Error('...');
              }
            })
            .then(function (doc) {
              if (doc.exists) {
                  // Here compare the answers and the correct responses
                  // For example
                  if (answerArray[0] === doc.data().Reponse1 && answerArray[1] === doc.data().Reponse2 && ...) {
    
                  }
              }
            }
            .catch(function (error) {
              console.log('Error getting document:', error);
            });
        } else {
          window.location.href = 'account-connexion.html';
        }
      });
    

    【讨论】:

    • 在第二个 .then() 中,在比较之前我做了一个 console.log(doc.data()),但我没有定义。我不明白。
    • 你确定有一个与db.collection("ReponseJ").doc("ReponseJuste")对应的文档吗?您是否具有读取权限(没有阻止访问的安全规则)?
    • 哦,我感觉自己真是个混蛋,很抱歉,文档名称缺少一个字符。无论如何,谢谢你的一切。
    • 每个开发者都会遇到这种情况 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多