使用带有Angular中的mergeMap的管道执行订单

时间:2018-11-10 03:11:17

标签: angular http

我是Angular的新手,因此绊脚石。 getIds返回一个ID数组,然后我们为每个ID创建一个Observable(getNames)。

interface Name{
    "Id": string;
    "": string;
    "cve_link": string;
    "cve_description": string;
    "cve_applications": string;
    "cve_hosts": string;
    }
export interface Vulns extends Array<Vuln>{}

getIds(): void {
 this.cvetservice
     .getIds().pipe(
     mergeMap(names => this.getNames(this.ids)),
     mergeMap(names => names),
     toArray()
     )
}

getNames(data: Ids):Observable<any[]> {
 return this.http
    .post(url,data)
    .pipe(map(({ Results }: any) => Results[0].results.map(item => ({Id: item.Id, Name: item.Name }))));
    }

第133 mergeMap(names => names)行无法编译(以下错误)。

ERROR in src/app/mcomponent.ts(133,19): error TS2345: Argument of type '(names: Name) => Name' is not assignable to parameter of type '(value: Name, index: number) => ObservableInput<{}>'.
Type 'Name' is not assignable to type 'ObservableInput<{}>'.
Type 'Name' is not assignable to type 'Iterable<{}>'.
Property '[Symbol.iterator]' is missing in type 'Name'.
src/app/mcomponent.ts(138,28): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.

有人可以帮忙弄清楚我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

乍一看您的代码。这里是问题

getIds(): Observable<any[]> {
 return this.cvetservice
     .getIds().pipe(
     mergeMap(names => this.getNames(this.ids)),
     mergeMap(names => names),
     toArray()
     )
}

getNames(data: Ids):Observable<any[]> {
 return this.http
    .post(url,data)
    .pipe(map((Results : any) => Results[0].results.map(item => ({Id: item.Id, Name: item.Name }))));
    }

提供JSON响应,然后stackblitz将增加更多价值。