【问题标题】:undefined is not an object (evaluating 'userCredentails.user')undefined 不是一个对象(评估'userCredentails.user')
【发布时间】:2021-12-15 11:04:51
【问题描述】:

我正在为我的 react 本机应用程序构建一个用户身份验证页面。问题我在注册用户时无法更新 displayName。之后,我需要将姓名、电子邮件、密码存储在我的 Firestore 数据库中。

我正在使用 expo 和 firebase。我不明白为什么它不起作用。

import firebase from "firebase";
import { auth } from "../../firebase";

const SignUpScreen = () => {
  const [name, setName] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  // const user = firebase.auth().currentUser;

  // Sign Up ----------
  const handleSignUp = () => {
    auth
      .createUserWithEmailAndPassword(email, password)
      .then((userCredentials) => {
        if (userCredentials) {
          const user = userCredentials.user;
          user.updateProfile({
            displayName: name,
          });
        }
      })
      .then((userCredentials) => {
        const user = userCredentials.user;
        // console.log("Registered with:", user.email);
      })
      .catch((error) => alert(error.message));
  };

【问题讨论】:

    标签: reactjs firebase react-native google-cloud-firestore firebase-authentication


    【解决方案1】:

    第一个 then() 块似乎没有返回任何内容。 updateProfile() 函数也返回一个承诺。尝试用 async-await 语法重构代码,如下所示:

    const handleSignUp = async () => {
      try {
        const userCredential = await auth.createUserWithEmailAndPassword(email, password)
        const user = userCredential.user
      
        await user.updateProfile({ displayName: name })
      
        console.log("Registered with:", user.email);
      } catch (e) {
        console.log(e)
      }
    }
    

    【讨论】:

    • 虽然没有显示任何错误,但 displayName 没有更新。
    • @BISHALSAHA 您如何记录更新后的名称?你能分享更新的代码吗?
    • 我只是控制台记录用户对象。电子邮件、显示名称、照片、电话等所有内容都包含在用户对象中。我尝试了你的方法,但它没有工作,但没有错误
    • @BISHALSAHA 你能在 updateProfile 之后尝试console.log(auth.currentUser) 吗?是否包含更新的名称?
    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2022-09-22
    • 1970-01-01
    • 2018-11-12
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多