简体   繁体   English

Angular 2转换为Promise

[英]Angular 2 converting to Promise

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". 我想从像API一样工作的API中获取数据,但是我正努力通过“可观察”中的给定ID过滤单个报告。

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>'"
}
  1. getAllReports() is responsible for the communication with the API and returns an Ovserable getAllReports()负责与API的通信并返回一个Ovserable
  2. getReports() inserts the results from the API call in a reports: Report[] aray getReports()将API调用的结果插入reports: Report[]
  3. getSingleReport() should return a Promise<Report> with the given Observable<Report[]> getSingleReport()应该返回一个Promise<Report>和给定的Observable<Report[]>
  4. getSingleReport1() is my first try fixing the problem which unfortunately also doesn't work getSingleReport1()是我的第一个尝试解决该问题的方法,不幸的是它也无法正常工作

Of course i know where the problems in 3 & 4 are but in don't know how to solve them. 我当然知道3和4中的问题在哪里,但不知道如何解决。

How can i accomplish a conversation from Report or Observable<Report[]> to Promise<Report> . 我如何完成从ReportObservable<Report[]>Promise<Report>的对话。

Any help is highly appreciated. 非常感谢您的帮助。

Thank you. 谢谢。

Use toPromise operator like this: 像这样使用toPromise运算符:

getSingleReport(id: number): Promise<Report> {
    return this.getAllReports()
        .subscribe(reports => reports.find(report => report.id === id)).toPromise();
}

So i still didn't find a solution but i came up with a workaround i'll post here. 所以我仍然没有找到解决方案,但是我想出了一种解决方法,我将在此处发布。 I changed getSingleReport(id: number): Promise<Report> to getSingleReport(id: number): Report . 我将getSingleReport(id: number): Promise<Report>更改为getSingleReport(id: number): Report

getSingleReport(id: number): Report {
    this.getAllReports()
        .subscribe(
            reports => this.reports = reports,
            error => console.log(error)
        );
    return this.reports.find(report => report.id === id);
}

This works but is still throwing the error TypeError: Cannot read property 'find' of undefined . 此方法有效,但仍会TypeError: Cannot read property 'find' of undefined错误TypeError: Cannot read property 'find' of undefined I'll probably fix it later today. 我可能会在今天晚些时候修复它。

UPDATE: 更新:

I got it perfectly working now but i decided to do it without a Promise. 我现在可以完美地工作了,但是我决定不做任何承诺。

report-detail.component.ts:
getReport(id: number) {
    this.reportService.getSingleReport(id)
        .subscribe(
            report => this.report = report,
            error => console.log(error)
        );
}

report.service.ts:
getSingleReport(id: number): Observable<Report> {
    const apiUrl = 'http://localhost:8080/Berichtsprogramm-API/getReport?api_key={TEMPORARY_APIKEY}&id=' + id;
    return this.http.get(apiUrl)
            .map(res => res.json());
}

This works for my case and finally i can be happy again. 这适用于我的情况,最后我可以再次感到高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM