【发布时间】:2023-03-28 01:39:01
【问题描述】:
我有一个简单的系统,带有 Firebase 的用户身份验证系统。我想接收当前登录用户的数据列表。我通过订阅 Firebase 的 observable 从当前登录用户那里获取信息。
但是,在调用getAllEmails() 函数之前,不会调用在我的构造函数中设置的private authId 的值。因此${this.authId} 产生未定义。在调用 getAllEmails 函数之前,如何确保已从 observable 中准备好 authId 的值?
import { Injectable } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import { Email } from '../email';
import { AuthService } from '../auth/auth.service';
@Injectable()
export class EmailService {
private authId: string;
constructor(
private af: AngularFire,
private authService: AuthService
) {
this.authService.getUserInformation()
.subscribe(user => {
this.authId = user.authId;
})
}
getAllEmails(): FirebaseListObservable<any> {
return this.af.database.list(`/emails/${this.authId}`);
}
}
【问题讨论】:
-
你在哪里调用 getAllEmails()? ngOnInit()
-
是的,然后将其分配给类变量
标签: angular firebase observable