ngOnInit(): void {
this.store.dispatch(new patients.Load([]));
this.patients$ = this.store.select(fromPatients.getAll);
this.patients$.map(p =>{ // patients$: Observable<Patient[]>;
this.rows = p.map(pat => { //I use this on the front end
return {
username: pat.username,
id: pat.id,
hasAlert: this.hasAlerts(pat), //made this an observable for the async pipe in view
settings: "Settings"
};
});
this.table.recalculatePages();
console.log(this.rows);
console.log("This happens first");
}).subscribe();
}
hasAlerts(pat: Patient): Observable<boolean> {
var shouldAlert$ = Observable.of(false);//this value is always taken
this.observations2$ = this.dataService.fetchItems<Observation>(
"Observation",
null,
pat.id// How would i pass this in with mergeMap()?
);
this.observations2$.subscribe(curObservation => {
if (curObservation.category.coding[0].code == "GlucoseEvent"){
shouldAlert$ = Observable.of(true);
console.log("should alert$", shouldAlert$);
}
});
console.log("this happens second");
return shouldAlert$;
}
In the code above I parse an observable called patients$ that has an array of patients. I am then mapping those patients to an object array called this.rows that I show on my client.
My problem involves the hasAlert property which processes another observable itself in the method call to hasAlerts(). This method call to hasAlerts() does not happen synchronously so the console.log("this happens first"); occurs before my hasAlerts method can do the logic in the If statement to decide whether it should be set to true or false, instead it just uses the value it is initialized to in the first line of hasAlerts(). Confirmed by the console.log("this happens second"); being displayed second.
hasAlerts() could return a boolean not an observable, I was trying to see if that would solve my problem using an asycn pipe on the front end (it didnt).
I believe the way to solve this involves using mergemap however I am not sure how i would pass in the pat.id that the hasAlerts method needs? Or maybe this is not the correct approach to solve my current problem with the asynchronous execution of this code.
I am currently trying to use this this question about mergemap to solve my issue but passing the pat.id that the second observable in hasAlerts I haven't figured out yet. 1
updated code following Piccis idea.
this.patients$.map(p =>{ // patients$: Observable<Patient[]>;
this.rows = p.map(pat => { //I use this on the front end
return {
username: pat.username,
id: pat.id,
hasAlert: false, //set the default value
settings: "Settings"
};
})
}).do(data => console.log("data1",data))
// .switchMap(p => Observable.from(p))
// .do(data => console.log("data2",data)) // creates a stream of Observable<Patient>
// .mergeMap(patient => this.dataService.fetchItems<Observation>(
// "Observation",
// null,
// "pat..frank"//patient[0].id//"pat..frank"//patient.id// patient should be your guy
// )
// .map(curObservation => {
// console.log("currOBS",curObservation);
// if (curObservation.category.coding[0].code == "GlucoseEvent"){
// var shouldAlert$ = true;
// console.log("should alert$", shouldAlert$);
// }
// })
// ).do(data => console.log(data))
// .toArray()
.subscribe(
patients => {
this.table.recalculatePages();
console.log(this.rows);
}
)
Data1 returns an array of patients. I need to comment out the middle because there is a syntax error with the switchmap line saying " Argument of type 'void' is not assignable to parameter of type 'ArrayLike<{}>'"