【问题标题】:how to add users to firestore using the same id after authentication如何在身份验证后使用相同的 ID 将用户添加到 Firestore
【发布时间】:2021-12-05 09:53:06
【问题描述】:
const handleSignup = ()=>{
    auth()
    .createUserWithEmailAndPassword(email, password)
    .then((cred) => {
        console.log(cred);
        firestore()
        .collection('users')
        .doc(cred.uid)
        .set({
          display_name:username
        })
        .then(() => {
          console.log('User added!');
        });
    })
    .catch(error => {
        console.log(error.code);
        if (error.code === 'auth/user-not-found') {
          console.error('Invalid Email or password');
        }else
          console.error('Sorry an error occured');
    });
  };

我正在使用上面的代码将用户详细信息存储在 firestore 中,并且我希望文档与身份验证后生成的凭据 uid 相同。但文档中的 id 与 cred.uid.How to 完全不同解决这个问题?

【问题讨论】:

  • cred console.log 显示什么?我不是 100% 确定,但是 uid 不是存储在 cred.user.uid 中吗? (firebase.google.com/docs/auth/web/password-auth 似乎暗示了同样的事情:使用cred.user
  • 我的错!它现在正在工作。谢谢。
  • 听起来像一个答案@Berci ???? :)

标签: javascript react-native firebase-authentication


【解决方案1】:

正确的语法是cred.user.uid(而不是cred.uid)。有关此主题的来源和更多信息,请查看firebase password auth docs

PS:这已经在 cmets 中得到了回答,但我也在这里发布了回复以便更容易发现(并建议问题有答案。谢谢@Frank van Puffelen建议)。

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2021-11-02
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多