我正在开发一个应用程序,我想实现这一目标。当用户登录时,我想获取其用户名并存储在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
Doc ref:
更新用户的个人资料
您可以更新用户的基本个人资料信息-用户的显示名称和个人资料照片的网址-使用updateProfile方法。例如:
var user = firebase.auth()。currentUser;
user.updateProfile({displayName:“ Jane Q. User”,photoURL:“ https://example.com/jane-q-user/profile.jpg”})。then(function(){// 更新成功。 },function(error){//发生错误。 });
我的回答为时已晚,但以防万一有人感兴趣,您可以这样做
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