I have tested different solution such as adding map or switchMap but I'm lost, I still can't get the data out of the subscribe of the Service function.
I have the service function :
getAllExperiments (): Observable<Experiment[]> {
return this.http.get<Experiment[]>(this.urlEndPoint)
.pipe(
catchError(this.handleError<Experiment[]>('getExperiments', []))
);
}
and the call in the ngOnInit
:
async ngOnInit() {
await this.experimentService.getAllExperiments()
.subscribe( response => {
this.expTable = response
this.expId = this.expTable.length;
console.log(this.expTable, this.expId) //appears after the other console.log but with the values
})
console.log(this.expId) //appears first and undefined
await this.experimentService.getExperiment(this.expId)
.subscribe(response => {
this.experiment = response
});
await this.compareService.getScore(this.expId)
.then(response => {
this.expScore = response;
})
}
}
And obviously the function getExperiment
with the ID doesn't work as well.
I know it should be able to work with a toPromise
but I don't see how to use it here.