【发布时间】:2019-02-05 08:15:48
【问题描述】:
我有我的 AuthService,我订阅了 authState。
@Injectable()
export class AuthService {
private user: User = null;
constructor(private afAuth: AngularFireAuth) {
const sub$ = this.afAuth.authState.subscribe(auth => {
this.user = auth;
});
get userId(): string {
return this.user.uid;
}
}
现在在其他组件中,我想获取属于当前登录用户的对象,所以我创建了一个服务:
@Injectable()
export class SomeService{
constructor(private readonly afs: AngularFirestore,
private auth: AuthService) {
}
...
getByCurrentUser(): Observable<any> {
return this.afs.collection<any>('my-path',
ref => ref
.where('userId', '==', this.auth.userId)).valueChanges();
}
}
在组件中我订阅了这个方法:
...
ngOnInit() {
this.getData();
}
private getData(): void {
this.testService.getByCurrentUser().subscribe(
res => console.log(res),
error => console.log(error)
);
}
问题:
当我在页面之间重定向时它工作正常,但是在刷新页面之后getData() 方法在authState 回调分配当前身份验证之前调用,实际上userId() 方法返回null。
如何预防?
【问题讨论】:
标签: angular firebase firebase-authentication angularfire2