I want to fetch data from an API which works like a charm but i am struggling with filtering a single Report by a given id from an "Observable".
Here are some snippets:
getAllReports(): Observable<Report[]> {
return this.http.get(this.reportUrl)
.map(res => res.json().results);
}
getReports(): void {
this.reportService.getAllReports()
.subscribe(
reports => this.reports = reports,
error => console.log(error)
);
}
getSingleReport(id: number): Promise<Report> {
return this.getAllReports()
.then(reports => reports.find(report => report.id === id));
// ^ "Property 'then' does not exist on type 'Observable<Report[]>'"
}
getSingleReport1(id: number): Promise<Report> {
this.getAllReports()
.subscribe(
reports => this.reports = reports,
error => console.log(error)
);
return this.reports.find(report => report.id === id);
// ^ "Type 'Report' is not assignable to type 'Promise<Report>'"
}
getAllReports()
is responsible for the communication with the API and returns an OvserablegetReports()
inserts the results from the API call in areports: Report[]
araygetSingleReport()
should return aPromise<Report>
with the givenObservable<Report[]>
getSingleReport1()
is my first try fixing the problem which unfortunately also doesn't work
Of course i know where the problems in 3 & 4 are but in don't know how to solve them.
How can i accomplish a conversation from Report
or Observable<Report[]>
to Promise<Report>
.
Any help is highly appreciated.
Thank you.