【问题标题】:firestore set operator do not add any documentfirestore set operator 不添加任何文档
【发布时间】:2021-01-15 11:58:57
【问题描述】:

我创建了一个承诺链来按顺序将用户存储到 Firestore 和身份验证。第一次,我使用 add 运算符,但我决定我应该根据他们的 uid 存储用户,因为这个原因我改变了我的代码,现在没有任何东西保存到 firestore。怎么了?

 firebase
            .auth()
            .createUserWithEmailAndPassword(email, password)
            .then((data) => {
              firebase
                .firestore()
                .collection("Users")
                .doc(data.user.uid)
                .set({
                  name: name,
                  surname: surname,
                  email: email,
                  password: password,
                  nickname: nickname,
                  birthdate: birthdate,
                })
                .then(() => {
                  firebase
                    .auth()
                    .currentUser.sendEmailVerification()
                    .then(
                      firebase
                        .auth()
                        .signOut()
                        .then(
                          navigation.navigate("SignIn", {
                            message: "Please verify your email.",
                          })
                        )
                    );
                })
                .catch((e) => console.log(e));
            })
            .catch((err) => console.log(err));

注意:身份验证工作正常。

注意2:控制台没有错误。

注 3:我还注意到,如果我更改了顺序,例如先存储 firestore,然后存储到身份验证代码,则可以正常工作。为什么按这个顺序什么都没发生。

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    解决办法是:

    auth
            .createUserWithEmailAndPassword(email, password)
            .then((data) => {
              console.log(data.user.uid);
              return usersRef.doc(data.user.uid).set({ abcxyz: "anything" });
            })
            .then(() => {
              auth.currentUser.sendEmailVerification();
            })
            .then(() => {
              auth.signOut().then(
                navigation.replace("SignIn", {
                  message: "Please verify your email.",
                })
              );
            })
            .catch((e) => console.log(e));
        } else {
          console.log(1232);
        }
      };
    

    还有裁判:

      const auth = firebase.auth();
      const db = firebase.firestore();
      const usersRef = db.collection("Users");
    

    如果我在每种情况下都使用firebase.firestore()firebase.auth(),我猜他们每次都会打开一个新函数,这不会创建 Promise。但是,如果我创建一个单一的参考,那么一切正常。

    【讨论】:

      【解决方案2】:

      异步 ​​Firebase 方法返回的 chaining the promises 不正确。在每个 then() 块中,您需要返回异步 Firebase 方法返回的 Promise,否则回调将无法捕获上一个 Promise 的结果。

      以下应该可以解决问题(未经测试):

           firebase
              .auth()
              .createUserWithEmailAndPassword(email, password)
              .then((data) => {
                return firebase     // <== see the return
                  .firestore()
                  .collection("Users")
                  .doc(data.user.uid)
                  .set({
                    name: name,
                    surname: surname,
                    email: email,
                    password: password,
                    nickname: nickname,
                    birthdate: birthdate,
                  });
               })
               .then(() => {
                    return firebase   // <== see the return
                      .auth()
                      .currentUser.sendEmailVerification();
               })
               .then(() => {
                    return firebase
                          .auth()
                          .signOut();
               })
               .then(() => {
                 navigation.navigate("SignIn", {
                     message: "Please verify your email.",
                 });
               })
               .catch((err) => console.log(err));
      

      还请注意,我们避免嵌套 then() 块,因为承诺链“最好保持扁平而不嵌套”:更多详细信息 here

      【讨论】:

      • 它跟随并跳转所有“then”但firestore仍然没有保存文档。
      • 没有。什么都没抓到。
      • 电子邮件验证也给我发一封电子邮件,createUserWithEmailAndPassword 工作正常,但 firestore 不保存数据
      • 如果我使用第一个保存的 firestore 更改了顺序,然后创建身份验证用户一切正常,但我需要身份验证中的 uid。
      • 您确定没有任何安全规则阻止文档创建吗?也 100% 确定您查看了正确的集合或项目(我知道这看起来很愚蠢,但它至少发生在我们所有人身上 :-) )它以相反的顺序工作很奇怪(我猜你通过了data.user.uid 的虚拟值)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2020-12-04
      相关资源
      最近更新 更多