在JavaScript中的Firebase Firestore更新功能中使用Promise

时间:2019-06-30 20:33:36

标签: javascript firebase promise google-cloud-firestore

我正在尝试在用户名更新完成后将用户发送到另一个页面。问题是在这种情况下,我不知道何时调用我的location.replace函数或如何使用Promise。 (username.value是新的用户名)

     //don't worry about the if statment, all it is doing is making sure that the element 
     //that stores the new username is not empty
     if(username.value != ""){
      db.collection("users").doc(doc.id).update({
        "name": username.value
      })  
      //How can I call this as soon as the username is set?
      //location.replace("../pages/homepage.html");
     }

1 个答案:

答案 0 :(得分:1)

您应该在这里找到一些东西:https://firebase.google.com/docs/firestore/manage-data/add-data

但是您应该这样做:

db.collection("users").doc(doc.id).update({
        "name": username.value
      }) 
      .then(() =>  location.replace("../pages/homepage.html"))