Home > database >  undefined is not an object (evaluating 'userCredentails.user')
undefined is not an object (evaluating 'userCredentails.user')

Time:11-02

I am building a user Authentication page for my react native app. Issues I can't update the displayName while signing up my user. After that I need to store the name, email,password in my Firestore database.

I am using expo and firebase. I can't understand why it is not working.

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));
  };

CodePudding user response:

The first then() block doesn't seem to be returning anything. Also updateProfile() function returns a promise. Try refactoring the code in async-await syntax as shown below:

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)
  }
}
  • Related