首页 > 解决方案 > 尝试在使用 Firebase 创建帐户时创建用户文档时出现“无法读取未定义的属性‘用户’”

问题描述

我试图在创建帐户时将用户 ID 添加到一个名为“帐户”的新集合中(其中也将包含 displayName)。但是,我不断收到“无法读取未定义的属性‘用户’”错误。我究竟做错了什么?

// Sign up
           try {
               setError("")
               setLoading(true)
               await signup(emailRef.current.value, passwordRef.current.value)
                   //Set displayname
                   //This part works
                   .then((result) => {
                       return result.user.updateProfile({
                           displayName: displayNameRef.current.value
                       })
                   })
                   //Set user id
                   //"Cannot read property 'user' of undefined" ?
                   .then((result) => {
                       return accounts.doc(result.user.uid).set({
                           userID: result.user.id
                       });
                   });
               history.push("/")
           }

标签: javascriptreactjsfirebasejsx

解决方案


尝试插入这个而不是你trySignup.js

try {
      setError("");
      setLoading(true);
      const result = await signup(
        emailRef.current.value,
        passwordRef.current.value
      );
      //Set displayname
      //https://medium.com/@doyinolarewaju/firebase-adding-extra-information-to-user-on-sign-up-and-other-tips-4ebe215866e
      await result.user.updateProfile({
        displayName: displayNameRef.current.value
      });

      console.log(result);

      //Set user id
      await accounts.doc(result.user.uid).set({
        userID: result.user.id
      });
      history.push("/");
    }

推荐阅读