【问题标题】:Fail to update field in FireStore无法更新 FireStore 中的字段
【发布时间】:2020-05-23 10:05:10
【问题描述】:

我正在开发一个连接到 FireStore 的项目。我试图更新集合 User 内文档 userIDKey 中的一个字段。它通过函数运行而不会引发错误。但是,它不会更新 FireStore 中的字段。谁能告诉我我哪里做错了?下面的附件是我的代码、我的 FireStore 数据库的图像和演示更新字段。

Demo Image

FireStore "infected" field not updating after running through the function

firebase.auth().onAuthStateChanged((user) => {
                    if (user) {
                        console.log(user.uid);
                        userID = user.uid;

                        if ((check_bolean != "yes") && (check_bolean != "no")){
                            alert("Please input YES/NO for the following box!");
                        }
                        else{
                            if (check_bolean == "yes"){
                                fStore.collection("User").doc(userID).update({
                                    infected: true
                                })
                                alert("You are infected")
                            }
                            else{
                                fStore.collection("User").doc(userID).update({
                                    infected: false
                                })
                                alert("You are not infected!");
                            }
                            window.location.assign("MainPage.html")
                        }
                    } else {
                        console.log("Cannot get userid")
                    }
                });

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您应该在 api 调用中添加回调侦听器,以便获得成功或错误响应。

    alert("你没有被感染!") 被调用,因为它不在任何回调方法中。所以你应该像这样改变你的代码。

    fStore.collection("User").doc(userID)
    .update({
           infected: false
    }).then(function() {
        // update successful here
        alert("You are not infected!");
    }).catch(function(error) {
         console.log(error);
    })
    

    另外,请确保您已将 Firestore 规则设置为接受更新事务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 2018-05-27
      • 2018-06-11
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多