【发布时间】:2020-03-06 11:55:13
【问题描述】:
我正在使用 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'
【问题讨论】:
标签: javascript angular firebase google-cloud-firestore firebase-authentication