Could someone please clarify why the variable this.userDataObserver is undefined, causing the .next() function to fail? I'm quite puzzled by this.
I am in need of pushing some data into a service variable that can be accessed from other components (I am looking to avoid using local storage).
If anyone could shed some light on why this variable is undefined, despite being clearly set in the class, it would be greatly appreciated.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import 'rxjs/add/operator/map';
@Injectable()
export class globalService {
//Sets logon token
userData:any;
userDataRetrieval: Observable<any>;
userDataObserver: any;
//Sets + updates user info
userInfo: any;
userInfoRetrieval: Observable<any>;
userInfoObserver: any;
constructor() {
this.userDataRetrieval = new Observable(observer => {
this.userDataObserver = observer;
})
this.userInfoRetrieval = new Observable(observer => {
this.userInfoObserver = observer;
})
}
setUser(userData: any) {
this.userData = userData;
this.userDataObserver.next(this.userData); The issue arises as this.userDataObserver turns out to be undefined when calling this function from my component.
}
saveUserInfo(userInfo: any) {
this.userInfo = userInfo;
this.userInfoObserver.next(this.userInfo);
}
}