我尝试更新用户个人资料,然后阅读this documentation。但是,为什么我无法改变我的displayName
?它总是给我null
。
它也给了我TypeError: userNow is null
。
我应该遵循哪一个更新/编辑用户个人资料?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Update User Profile</title>
<script src="https://www.gstatic.com/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.com/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>
&#13;
答案 0 :(得分:1)
将代码从var userNow = firebase.auth().currentUser;
开始包含在函数中。正如@Andre Kool所说,你需要首先确定你是否正确(因为它是异步操作然后尝试更新用户数据:
var authRef = firebase.auth();
authRef.onAuthStateChanged(function(user) {
if (user) {
console.log('Display name onAuthStateChanged : '+user.displayName);
_updateUserData();
} else {
console.log('not login');
}
});
fucntion _updateUserData(){
var userNow = firebase.auth().currentUser;
userNow.updateProfile({
displayName: "Jane Q. User",
photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(function() {
var displayName = userNow.displayName;
var photoURL = userNow.photoURL;
}, function(error) {
});
}