angular - enforce order using pipe with mergeMap in Angular
问题描述
I'm a newbie to Angular and stumbling with this. getIds returns an array of Ids and then we create an Observable(getNames) for each of these Ids.
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 }))));
}
The line 133 mergeMap(names => names)
fails to compile (error below).
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.
Can someone help clarify what I'm doing wrong here?
解决方案
乍一看您的代码。这是问题
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 将增加更多价值。
推荐阅读
- php - "Error": "Unrecognized field: 0 when try to findBy ASC ORM/Doctrine
- google-people-api - 使用谷歌人员服务的人员 API 搜索联系人
- ios - xcode 9上的alamofire pod问题
- javascript - 正则表达式捕获超链接
- ethernet - 将 enc28j60 以太网模块与 esp32 连接
- email - 无法与主机 smtp.gmail.com 建立连接:stream_socket_client():
- react-native - 如何在本机反应中使用两个不同的 DateTimePicker 查找两个日期之间的差异
- python - Python Pandas Group by 并添加带有数字序列的新行
- c++ - dart 和 Qt/C++ 之间的 TCP 套接字连接关闭连接,删除旧的 QtcpSocket 对象
- javascript - Radio 组件内的 Select Dropdown 短暂出现后立即消失