Firebase firestore .set(data) 承诺在我退出时解决

我正在尝试为我的登录组件设置一个 SignUp 方法,当我第一次使用 Firebase 的 SignUp 方法时,它似乎工作正常。当我第一次注销这个新创建的用户并尝试使用电子邮件和密码创建另一个用户(不刷新浏览器)时出现问题,这个用户不会立即在 firebase firestore 数据库中更新,而是承诺将存在但它只会在我单击我的注销方法时解决到数据库。(如果我刷新浏览器就可以了)


这是我的注册功能


const signUp = (e) => {

e.preventDefault();

FirebaseApp.auth()

  .createUserWithEmailAndPassword(email, password)

  .then((cred) => {

    console.log('We are inside the .then')

    FirebaseApp.firestore()

      .collection("Users")

      .doc(cred.user.uid)

      .set({

        Name: name,

        LastName: lastname,

        Email: email,

      })

      .then(function () {

        console.log("Stored!");

      })

      .catch(function (error) {

        console.log(error);

      });

  })

  .catch(function (error) {

    console.log(error)

  }); };

我在我的第一个 .then 中使用了一个 console.log 来检查我是否确实正在经历成功过程,然后我得到了日志,我在第二个 .then 中使用了一个 console.log 。然后检查设置到 db 的数据. 我得到日志,


之后我注销并尝试创建第二个用户,但从现在开始,它只会在我注销时设置数据,而不是在我使用我的注册方法时设置数据。


这是我的 SignOut 功能


<Button

      type="submit"

      fullWidth

      variant="contained"

      color="primary"

      className={classes.submit}

      onClick={() => {

        FirebaseApp.auth().signOut();

      }}

    >

      Sign Out

    </Button>

我尝试的最后一件事是使用 setTimeout 方法更改我调用与 createUserWithEmailAndPassword 方法平行的 .set 的位置,它可以工作但不是最佳的,因为如果在创建过程中存在延迟,则数据不会有用户id 可用。或者最糟糕的是,如果在注册后正在获取数据它一开始是不可见的。


提前致谢。


FFIVE
浏览 150回答 1
1回答

江户川乱折腾

考虑到您的情况,我建议您稍微更改将经过身份验证的信息发送到 Firestore 的方式。您的代码应如下所示。export const signupUser = (userDetails) => {&nbsp; &nbsp; const {firstName, lastName, email, password} = userDetails&nbsp; &nbsp; return () => {&nbsp; &nbsp; &nbsp; &nbsp; firebase.auth().createUserWithEmailAndPassword(email, password)&nbsp; &nbsp; &nbsp; &nbsp; .then(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firebase.firestore().collection('users').doc(firebase.auth().currentUser.uid)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .set({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstName: firstName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastName: lastName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email: email&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .catch(error => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Something went wrong with added user to firestore: ', error);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .catch(error => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Something went wrong with sign up: ', error);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },};虽然此代码未经测试,但它基于这篇非常好的文章之一:如何验证用户身份并在 Cloud Firestore 中创建相应的文档。除了上述选项之外,您还可以尝试使用云功能,一旦创建用户,它将向 Firestore 发送信息。export const newAccountCreated = functions.auth.user().onCreate(user => {&nbsp; &nbsp; userDoc = {'email' = user.data.email,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'displayName' = user.data.displayName}&nbsp; &nbsp; admin.firestore().collection('users').doc(user.data.uid)&nbsp; &nbsp; .set(userDoc).then(writeResult => {&nbsp; &nbsp; &nbsp; &nbsp; console.log('User Created result:', writeResult);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }).catch(err => {&nbsp; &nbsp; &nbsp; &nbsp;console.log(err);&nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; });});使用 Cloud Funcrion 也可能是一种替代方法,因为它会在您每次创建用户时运行。这两个选项应该可以帮助您创建登录用户,并帮助您避免在尝试创建新用户时需要注销的问题。让我知道这些信息是否对您有帮助!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript