在阅读了这么多帖子和空的(或不是?)解决方案之后,我认为最好的办法就是发布我自己的帖子。所以我的目标只是获取用户的显示名称,这样在帖子列表或其他内容中,作者身份的 uid 看起来并不难看...... 以下是我发现与该问题相关的内容:
注册响应式(Reactive)表单,稍后在 onInit 上调用:
createForm() {
this.registerForm = this.fb.group(
{
name: ['', Validators.required],
email: ["", [Validators.required]],
password: [null, [Validators.required, Validators.minLength(5)]],
retype: ["", Validators.required]
},
{
validator: PasswordValidators.passwordsShouldMatch
}
);
}
提交方法:
onSubmit() {
if (this.registerForm.invalid) return;
this.registered = true
this.authService.registerUser(this.email.value, this.password.value).then(data=>{
console.log(data)
})
}
authService调用uid和register方法:
constructor( private afAuth: AngularFireAuth, private router: Router) {
this.afAuth.authState.subscribe(user=>{
this.userId = user.uid;
})
}
registerUser(email:string, password:string) {
return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then((result) => {
this.router.navigate(['/login']);
console.log(result.user)
}).catch((error) => {
window.alert(error.message)
})
}
记录此行时 this.afAuth.authState.subscribe(user=>{
this.userId = user.uid;
,实际上记录用户,我可以看到整个对象和displayName,但由于register方法只接受2个参数,电子邮件和pass,我该如何解决这个问题?我尝试过处理 updateprofile 的方法,但遇到错误......有什么解决方案吗?谢谢
最佳答案
如果您想要设置用户帐户的显示名称,则在使用 createUserWithEmailAndPassword 创建帐户时将无法执行此操作。您必须对updateProfile()执行后续调用在您返回的用户对象上。
result.user.updateProfile({
displayName: ...
})
.then(...)
.catch(...)
显然,您必须在调用 registerUser()
时传递名称。
关于javascript - Angular 8/Firebase : how do I set the displayName when creating user with email and password?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60405997/