Javascript firestore set操作员不添加任何文档

Javascript firestore set操作员不添加任何文档,javascript,node.js,firebase,google-cloud-firestore,Javascript,Node.js,Firebase,Google Cloud Firestore,我已经创建了一个承诺链,将用户存储到firestore,并在订单中进行身份验证。第一次,我使用了add操作符,但我决定根据用户的uid来存储用户,因为我更改了代码,现在没有任何内容保存到firestore。怎么了 firebase .auth() .createUserWithEmailAndPassword(email, password) .then((data) => { fireb

我已经创建了一个承诺链,将用户存储到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:我还注意到,如果我更改了顺序,比如先将store firestore更改为store,然后将store更改为authentication code,就可以了。为什么按这个顺序什么都没有发生。

异步Firebase方法错误地返回了您。在每个
then()
块中,您需要返回由异步Firebase方法返回的承诺,否则回调将无法捕获以前承诺的结果

以下方法可以实现此目的(未经测试):

firebase
.auth()
.createUserWithEmailAndPassword(电子邮件,密码)
。然后((数据)=>{
返回火基//{
返回火基//{
返回火基
.auth()
.signOut();
})
.然后(()=>{
导航。导航(“登录”{
信息:“请验证您的电子邮件。”,
});
})
.catch((err)=>console.log(err));

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

解决方案是:

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()
,我猜它们每次都会打开一个新函数,这并不会产生承诺。但是如果我创建一个引用,那么一切都会正常工作。

它会跟随并跳转“then”但是firestore仍然没有保存文档。您是否在catch块中看到错误?没有。未捕获任何内容。此外,电子邮件验证向我发送电子邮件,createUserWithEmailAndPassword工作正常,但firestore没有保存数据如果我更改了第一个保存的firestore的顺序,然后创建身份验证用户,一切正常,但我没有从身份验证中删除uid。
  const auth = firebase.auth();
  const db = firebase.firestore();
  const usersRef = db.collection("Users");