无法更新FireStore中的字段

时间:2020-05-23 10:05:10

标签: javascript firebase google-cloud-firestore

我正在研究一个连接到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")
                    }
                });

1 个答案:

答案 0 :(得分:2)

您应将回调侦听器添加到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规则以接受更新交易。