React Enjoyer
React Enjoyer

Reputation: 1402

How to properly update user information in firebase?

I have a code that is a form, basically is suppose to update the user information. According to Firebase documentation manage user but for some reason I'm getting this errors

enter image description here

This is what I have I don't think I'm missing anything is pretty much the same The only difference is that on my firebase.js I made new cons to make codes shorter but that shouldn't affect at all.

firebase.js

const db = firebase.firestore();
const auth = firebase.auth();
const storage = firebase.storage();

export {db, auth, storage};

Code

import {auth} from "./firebase"

const authUser = auth.currentUser
    const update = (e) => {
        e.preventDefault();
        authUser.updateProfile({
                displayName: {fullName}
        })
    }

I just wanted to test it on the user name first and then do all the others but I don't understand why is not working is super simple.

Update

enter image description here

Upvotes: 1

Views: 179

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76551

authUser is undefined. This means that auth.currentUser was undefined at the time when it was assigned to authUser. This probably means that the user is not logged in. You will need to handle the case when the user is not logged in, like

import {auth} from "./firebase"

const authUser = auth.currentUser
    const update = (e) => {
        e.preventDefault();
        
        if (authUser) {
            authUser.updateProfile({
                displayName: fullName
            })
        } else {
            //User is not logged in, handle that case here
        }
    }

Upvotes: 2

Jacob
Jacob

Reputation: 389

import {auth} from "./firebase";
const authUser = auth.currentUser;
const update = (e) => {
    e.preventDefault();
    authUser?.updateProfile({
        displayName: fullName
    });
}

Upvotes: 0

Related Questions