gpt4 book ai didi

angular - 注册 Firebase 期间的 UpdateProfile()

转载 作者:行者123 更新时间:2023-12-03 14:58:54 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我想实现这一目标。
当用户登录时,我想获取他的用户名并存储在 firebase 中。
所以我创建了这个:

Signup(email: string, password: string, displayName: string) {
this.angFire.auth.createUser({
email: email,
password: password
}).catch(
(err) => {
console.log(err);
this.error = err;
})
...

这有效,现在我还需要将 displayName 保存到当前用户中,所以我尝试在同一个函数 (Singup()) 中执行此操作:
let user = firebase.auth().currentUser;
user.updateProfile({
displayName: this.displayName,
photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(function() {
console.log("Nome utente inserito");
}, function(error) {
console.log("Errore durante inserimento utente");
});

但它给了我这个:
error_handler.js:54 EXCEPTION: Error in ./SignupPage class SignupPage - inline template:70:4 caused by: Cannot read property 'updateProfile' of null

怎么做?
我在 ionic 2 angular2, firebase/angulrfire 上运行

文档引用:

Update a user's profile

You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method. For example:

var user = firebase.auth().currentUser;

user.updateProfile({ displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(function() {
// Update successful. }, function(error) { // An error happened. });



Here

最佳答案

我的回答太晚了,但以防万一有人感兴趣,你可以这样做

  constructor(
private afAuth: AngularFireAuth,
private router: Router
) {
//
}


async signupUser(email: string, password: string): Promise<any> {
const data = await this.afAuth.createUserWithEmailAndPassword(
email,
password
);
return this.updateUserData(data.user).then(() => {
this.router.navigate(["/"]);
});
}

private updateUserData({ uid, email, photoURL, displayName }: User) {
const userRef: AngularFirestoreDocument<User> = this.afs.doc(
`users/${uid}`
);
return userRef.set({ uid, email, photoURL, displayName }, { merge: true });
}

您可以了解更多 here

关于angular - 注册 Firebase 期间的 UpdateProfile(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42930014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com