Angular RXJS:如何对相同的API进行多次调用,并标记并组合结果
我正在对同一API进行多个呼叫(具有不同的有效负载),然后将所有这些调用的结果(所有对象阵列)合并在一起。 我已经成功实现了这一目标。
但是,我需要标记每个结果数组的这些结果(每个对象),以便以后可以过滤它们,
即来自API调用1的对象,我可以添加一个属性,例如,api_id as api_id as api_id as api_id
as this this,object从API呼叫2的结果将具有API_ID:“ 2”, API_ID:“ 3”用于第三个API呼叫等等...
public GetTriplets(args: {payload: any, limit: number}[]) {
const urls = [];
args.forEach(arg =>
urls.push(this.http.post(environment.url+ `/data/docs?limit=${arg.limit}`, arg.payload))
);
return merge(...urls).pipe(
reduce((result:[], response:[]) => result.concat(response)),
mergeMap(
(res: []) => res && res.length > 0 ?
of(res)
: throwError('There are no docsfor the selected parameters.')
)
);
}
I'm making multiple calls (with different payloads) to the same API and then merging the results (all arrays of objects) of all these calls together.
I have acheived this much successfully.
However I need to mark these results (each object) of each result array so that I can filter them later,
i.e. For a object coming from API call 1, I could add a property, say api_id with value "1"
Like this, object from result of API call 2 would have api_id: "2",
api_id:"3" for 3rd API call and so on...
public GetTriplets(args: {payload: any, limit: number}[]) {
const urls = [];
args.forEach(arg =>
urls.push(this.http.post(environment.url+ `/data/docs?limit=${arg.limit}`, arg.payload))
);
return merge(...urls).pipe(
reduce((result:[], response:[]) => result.concat(response)),
mergeMap(
(res: []) => res && res.length > 0 ?
of(res)
: throwError('There are no docsfor the selected parameters.')
)
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并行发送多个http请求,然后在一个地方访问所有结果是
forkjoin
操作员。Sending multiple HTTP requests in parallel, then accessing all their results in one place is a good use-case for the
forkJoin
operator.