Javascript 在 Firebase 中,如何在 Auth 中更新用户的 displayName 字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39607023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:43:56  来源:igfitidea点击:

In Firebase, how do you update the displayName field of a user in Auth?

javascriptfirebasefirebase-realtime-databasefirebase-authentication

提问by imjared

I currently have a function that creates a user account. I'd like to be able to add a username field too, but I can't seem to figure out how to update that. I noticed in the Google console, that there is a displayName field that is set to null. I don't know how to change it. This is what I have:

我目前有一个创建用户帐户的功能。我也希望能够添加一个用户名字段,但我似乎无法弄清楚如何更新它。我注意到在 Google 控制台中,有一个设置为 null 的 displayName 字段。我不知道怎么改。这就是我所拥有的:

function create(email, password, username) {
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
        $timeout(function() {
            var errorCode = error.code;
            var errorMessage = error.message;
            console.log(errorCode.toUpperCase() + ":" + errorMessage);
        })
    });
    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            $timeout(function() {
                console.log("Success! Account Created!");
                user.displayName = username; /* this doesn't work*/
            });
        } else {}
    });
}

回答by imjared

firebase.auth.Authgives you a firebase.User. You should check out the methods you can run on User: https://firebase.google.com/docs/reference/js/firebase.User

firebase.auth.Auth给你一个firebase.User。您应该查看可以运行的方法Userhttps: //firebase.google.com/docs/reference/js/firebase.User

Seems like you're looking for updateProfile: https://firebase.google.com/docs/reference/js/firebase.User#updateProfile

似乎您正在寻找updateProfilehttps: //firebase.google.com/docs/reference/js/firebase.User#updateProfile