最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to update user profile in Firebase? - Stack Overflow

programmeradmin6浏览0评论

I try to update user profile and i read this documentation. But, why i can't change my displayName ?it always give me null.

And it also give me TypeError: userNow is null.

Which one i should follow to update / edit user profile ?

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Update User Profile</title>
        <script src=".11.0/firebase.js"></script>
        <script src="core.js">
        </script>
    </head>
    <body>
        <script>
            var authRef = firebase.auth();
            authRef.onAuthStateChanged(function(user) {
                if (user) {
                    console.log('Display name onAuthStateChanged : '+user.displayName);
                } else {
                    console.log('not login');
                }
            });
            
            
            var userNow = firebase.auth().currentUser;
            userNow.updateProfile({
                displayName: "Jane Q. User",
                photoURL: ".jpg"
            }).then(function() {
                var displayName = userNow.displayName;
                var photoURL = userNow.photoURL;
            }, function(error) {
                
            });

        </script>
        <script src="js/jquery-3.3.1.min.js"></script>
    </body>
</html>

I try to update user profile and i read this documentation. But, why i can't change my displayName ?it always give me null.

And it also give me TypeError: userNow is null.

Which one i should follow to update / edit user profile ?

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Update User Profile</title>
        <script src="https://www.gstatic./firebasejs/4.11.0/firebase.js"></script>
        <script src="core.js">
        </script>
    </head>
    <body>
        <script>
            var authRef = firebase.auth();
            authRef.onAuthStateChanged(function(user) {
                if (user) {
                    console.log('Display name onAuthStateChanged : '+user.displayName);
                } else {
                    console.log('not login');
                }
            });
            
            
            var userNow = firebase.auth().currentUser;
            userNow.updateProfile({
                displayName: "Jane Q. User",
                photoURL: "https://example./jane-q-user/profile.jpg"
            }).then(function() {
                var displayName = userNow.displayName;
                var photoURL = userNow.photoURL;
            }, function(error) {
                
            });

        </script>
        <script src="js/jquery-3.3.1.min.js"></script>
    </body>
</html>

Share Improve this question asked Apr 24, 2018 at 11:32 Ade GuntoroAde Guntoro 991 gold badge2 silver badges10 bronze badges 4
  • 1 Make sure your user is actually logged in before you try to update his profile. Remember firebase works asynchronous (including login) so it can take some time before an action is actually finished. – André Kool Commented Apr 24, 2018 at 11:46
  • hai @AndréKool , i already login from another page, and from onAuthStateChanged i can get all information from user, including email, uid, etc. – Ade Guntoro Commented Apr 24, 2018 at 12:23
  • where are you performing the login, could you post that code? Also, did you check if the name changes looking directly on firebase console (console.firebase.google.)? – Guilherme Lemmi Commented Apr 24, 2018 at 13:33
  • if userNow is null. than you did not log in successfully. Before to update the user detail, need to be sure logged in. – Cappittall Commented Apr 24, 2018 at 18:17
Add a ment  | 

3 Answers 3

Reset to default 9

Wrap your code starting var userNow = firebase.auth().currentUser; in a functions. And as @Andre Kool said, you need to be sure first you sidned in (as it is async operation then try to update user data as:

    var authRef = firebase.auth();
    authRef.onAuthStateChanged(function(user) {
        if (user) {
            console.log('Display name onAuthStateChanged : '+user.displayName);
            _updateUserData();
        } else {
            console.log('not login');
        }
    });
          
    function _updateUserData(){
      var userNow = firebase.auth().currentUser;
        userNow.updateProfile({
        displayName: "Jane Q. User",
        photoURL: "https://example./jane-q-user/profile.jpg"
      }).then(function() {
        var displayName = userNow.displayName;
        var photoURL = userNow.photoURL;
      }, function(error) {
        console.log(error)
      });
  }

use useState To grap The Value

 <input
      className="h-[30px] rounded-2xl p-5"
      placeholder="new name"
      onChange={(e) => setUpdateName(e.target.value)}
      type="text"
  />

 const [updateprofileUrl, setUpdateProfileUrl] = useState();
  const [updatename, setUpdateName] = useState();
  const updateProfileitems = async () => {
    try {
        await updateProfile(auth.currentUser, {
        displayName: updatename,
        photoURL: updateprofileUrl,
      });
       console.log("Profile Update");
    } catch (error) {
      console.log(error);
    }
  };

The more modern way to update the user profile is using the Firebase Web modular API. You need to be logged in first of course.

import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();
updateProfile(auth.currentUser, {
  displayName: "Jane Q. User",
  photoURL: "https://example./jane-q-user/profile.jpg"
});
发布评论

评论列表(0)

  1. 暂无评论