【发布时间】:2018-02-20 07:23:00
【问题描述】:
当我在作为 firebase.User 的 BehaviorSubject 上设置 next(..) 时出现运行时错误。空类型。
subscribe.js:165 TypeError: Cannot read property 'next' of undefined
这是导致问题的代码。
export class AppAuthService {
private currentUserSubject = new BehaviorSubject<firebase.User |null>(null);
public currentUser = this.currentUserSubject.asObservable().distinctUntilChanged();
private isAuthenticatedSubject = new ReplaySubject<boolean>(1);
public isAuthenticated = this.isAuthenticatedSubject.asObservable();
constructor(public afAuth: AngularFireAuth) {
afAuth.auth.onAuthStateChanged(this.onAuthStateChanged);
}
onAuthStateChanged(user: firebase.User): void {
if (user) {
this.currentUserSubject.next(user); <------err
this.isAuthenticatedSubject.next(true);
console.log('loggedIN!');
} else {
this.currentUserSubject.next(null); <-------err
this.isAuthenticatedSubject.next(false);
console.log('GONE!');
}
}
}
当this.currentUserSubject.next(..) 设置为用户obj 或null 时,该错误会随时出现。我怀疑是因为 currentUserSubject 是用 null 初始化的,而下一个 null 是错误的?...
private currentUserSubject = new BehaviorSubject<firebase.User |null>(null);
我无法使用new firebase.User() 对其进行初始化,因为它是一个接口而不是一个对象。
同时将 currentUserSubject 声明为主题会导致相同的错误。
private currentUserSubject = new Subject();
将 firebase.User 界面转换为 Subject/ReplaySubject 的最佳方法是什么?我正在尝试这样做,以便当用户更改配置文件组件上的某些内容时,它将在 firebase 服务器上动态更新。
谢谢。
【问题讨论】:
-
您的错误没有意义。我在这里试过,它有效:plnkr.co/edit/etWUx93vtQkfp0mkhbnU?p=preview 你确定你没有方法
set currentUserSubject(v) {某处吗?
标签: angular typescript firebase firebase-authentication rxjs