Home > Enterprise >  updateProfile is not a function (Firebase)
updateProfile is not a function (Firebase)

Time:01-24

I am learning React but when I register a user(using createUserWithEmailAndPassword) and try to update the displayName property I get the error as "user1.updateProfile is not a function".

How can I solve this error?

My code:

const register = async (e) => {
        if (name.length == 0) {
            alert("name cannot be empty");
        } else {
            const userWithEmailAndPassword = await createUserWithEmailAndPassword(auth, email, password)
                .then((auth1) => {
                    const user1 = auth.currentUser;
                    user1.updateProfile({
                        displayName: name
                    });
                })
                .catch(error => alert(error.message))
            console.log(userWithEmailAndPassword);
        }
    }

CodePudding user response:

The updateProfile function needs to be imported from Firebase Auth SDK directly when using the Modular SDK as shown below:

import { createUserWithEmailAndPassword, updateProfile } from "firebase/auth"

const register = async (e) => {
  if (name.length == 0) {
    alert("name cannot be empty");
  } else {
    const { user } = await createUserWithEmailAndPassword(auth, email, password)
    console.log(`User ${user.uid} created`)
    await updateProfile(user, {
      displayName: name
    });
    console.log("User profile updated")
  }
}
  •  Tags:  
  • Related