Firebase user.updateProfile({…}) not working in React App

后端 未结 2 1423
北恋
北恋 2021-01-20 01:56

So, I have this ReactJS app, there is a user database,

The function for creating the user is this

import { ref, firebaseAuth } from \'./../Componen         


        
2条回答
  •  长发绾君心
    2021-01-20 02:21

    I think the problem here is that you are confusing the user object in your database with the user in your authentication module. They are not the same.

    You save a 'copy' of your user to the database when you say the following in the first chunk.

    ref.child(`users/${user.uid}/info`)
        .set({
          email: user.email,
          uid: user.uid,
          number: ""
        })
    

    Then in the second chunk of code you try and update the current user in your authentication module. Not good. You should be updating your database, not your authentication module.

    var user = firebase.**auth()**.currentUser
    
    if (user != null) {
      user.updateProfile({...})
    }
    

    I don't think you can create a custom field on the current User in the authentication module. The updateProfile() is used to update the fields you get by default from the provider, such as email, display name, photoURL etc. You can't create new ones.

    You should update the copy of the user in your database and then reference that when you need the value of 'number'.

    You change function should probably be more like...

    changeNumberToNew = (n) => {
        var user = firebase.auth().currentUser;
        if (user) {
         ref.child(`users/${user.uid}/info`).update({number: n})
         .then(() => console.log("Number changer"))
         .catch(error => console.log(error))
        } else {
          console.log("No user")
        }
    }
    

提交回复
热议问题