Search code examples
javascriptfirebasevue.jsauthenticationvuex

createUserWithEmailAndPassword VUEJS error updating user profile


i have this issue with a firebase app i'm also developing , my intentions are to create an user and update his profile with a name using the firebase method createUserWithEmailAndPassword. I evolve the process and eventually it works but also throws an error which says kind of :

Uncaught TypeError: Cannot read property user of undefined
at eval (index.js?xxxx)
at e.g (auth.esm.js?xxx)
at kc (auth.esm.js?xxxxx)
at gc (auth.esm.js?xxxxxx)
at B.k.Zb (auth.esm.js?xxxxxx)
at Qb (auth.esm.js?xxxx)

despite of having already modified the user info, then i need to refresh the page to get this error to dissapear. Here part of my code:

        signUserUp({ commit }, payload) {
           commit("settingLoader", true);
           firebase
             .auth()
             .createUserWithEmailAndPassword(payload.email, payload.password)
             .then(() => {
               let user = firebase.auth().currentUser;
               console.log(user);
               user
                 .updateProfile({
                   displayName: payload.name
                 })
                 .then(usermod => {
                     const User = {
                     id:usermod.user.uid,              undefined usermod
                     email:usermod.user.email,         undefined usermod
                     name:usermod.user.displayName     undefined usermod
                   };
                   commit("settingUserIn", User);
                   commit("settingLoader", false);
                 });
             })
             .catch(error => {
               console.log(error);
               commit("settingLoader", false);
             });
         }

Then the error does reference to an eventual undefined "usermod" for user.uid, user.displayName ,and user.email. Any advice about what i'm missing? thanks in advance!!


Solution

  • https://firebase.google.com/docs/reference/js/firebase.User#updateprofile

    Firebase's user.updateProfile method returns a void promise, meaning it returns a promise with no value.

    You still have access to your user variable in your then, so why not just change it to

    ...
       user
                     .updateProfile({
                       displayName: payload.name
                     })
                     .then(() => {
                         const User = {
                         id: user.uid,              
                         email: user.email,        
                         name:user.displayName
                       };
                       commit("settingUserIn", User);
                       commit("settingLoader", false);
                     });
    ...