BehaviorSubject接下来没有定义

时间:2017-09-12 00:17:20

标签: angular typescript firebase firebase-authentication rxjs

当我在作为firebase的BehaviorSubject上设置next(..)时,我收到运行时错误。 null type。

{"error":"invalid_request","error_description":"Unable to retrieve access token: appid/redirect uri/code verifier does not match authorization code. Or authorization code expired. Or external member binding exists"}

以下是导致问题的代码。

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!'); } } } 设置为用户obj或null时,任何时候都会显示错误。我怀疑它是因为currentUserSubject初始化为null而下一个null是错误的?...

this.currentUserSubject.next(..)

我无法使用private currentUserSubject = new BehaviorSubject<firebase.User |null>(null); 初始化它,因为它是一个接口,而不是一个对象。

同时将currentUserSubject声明为主题会导致相同的错误。

new firebase.User()

将firebase.User接口转换为Subject / ReplaySubject的最佳方法是什么?我试图这样做,以便当用户更改配置文件组件上的内容时,它将在firebase服务器上进行动态更新。

感谢。

1 个答案:

答案 0 :(得分:1)

使用胖箭头功能:

constructor(public afAuth: AngularFireAuth) {
  afAuth.auth.onAuthStateChanged(user => this.onAuthStateChanged(user));
}