我正在使用Firebase,但遇到了一些麻烦。创建新用户时,我可以将其存储在数据库中,但是稍后,当访问另一个组件时,它将失败。
//Register a new user in our system
registerUserByEmail(email: string,pass: string) {
return this.afAuth.auth
.createUserWithEmailAndPassword(email,pass)
.then(res => {
this.email = res.user.email;
let user = {
email: this.email,goal: "",previousgoals: [],progress: {
accomplishedToday: false,completedgoals: 0,daysInRow: 0,unlockedBadges: []
}
};
// store in database
new Promise<any>((resolve,reject) => {
this.firestore
.collection("users")
.add(user)
.then(
res => {
console.log(res.id);
this.isAuthenticated = true;
this.router.navigate(["/dashboard"]);
},err => reject(err)
);
});
});
}
我相信这段代码基本上是作为用户注册电子邮件并将其成功存储到我的数据库中(已检查)。
尽管如此,当渲染home.component
或/dashboard
home.component
ngOnInit() {
this.setauthStatusListener();
this.getUser();
}
getUser() {
this.data.getUser().subscribe(user => {
this.user = user.payload.data();
});
}
data.service
getUser() {
return this.firestore
.collection("users")
.doc(this.currentUser.uid)
.snapshotChanges();
}
我收到以下错误 错误
TypeError:无法读取null的属性“ uid”
服务