angular - BehaviorSubject next not defined -
i getting runtime error when set next(..) on behaviorsubject firebase.user | null type.
subscribe.js:165 typeerror: cannot read property 'next' of undefined
here code thats causing issue.
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!'); } } }
the error shows anytime when this.currentusersubject.next(..)
set user obj or null. suspect because currentusersubject initialized null , next of null wrong?...
private currentusersubject = new behaviorsubject<firebase.user |null>(null);
i not able initialize new firebase.user()
because interface , not object.
also declaring currentusersubject subject results in same error.
private currentusersubject = new subject();
what best way convert firebase.user interface subject/replaysubject? trying when user changes on profile component dynamic update on firebase server.
thanks.
use fat arrow functions:
constructor(public afauth: angularfireauth) { afauth.auth.onauthstatechanged(user => this.onauthstatechanged(user)); }
Comments
Post a Comment