【发布时间】:2021-10-03 10:52:11
【问题描述】:
我需要对 2 个单独的端点进行查询,以提供我的组件所需的对象。
| No | Endpoint | Response |
|---|---|---|
| 1 | /registry/colors |
{"colors":["red","green","blue","orange"]} |
| 2 | /color/{colorName} |
{"name":"red", "details":["detail1","detail2", "detail3"]} |
对于颜色数组中的每种颜色,我需要调用颜色细节端点并将所有这些颜色细节收集到一个颜色数组中。
getColors(): Observable<Color[]> {
return this.http.get<ColorRegistry>(this.registryUrl).pipe(
map((registry:ColorRegistry) => registry.colors.concatMap((colorName: string) => {
return this.http.get<Color>(this.colorUrl+"/"+colorName);
}))
)
}
我希望上述方法返回以下内容:
[
{"name":"red", "details":["detail1","detail2", "detail3"]},
{"name":"green", "details":["detail1","detail2", "detail3"]},
{"name":"blue", "details":["detail1","detail2", "detail3"]},
{"name":"orange", "details":["detail1","detail2", "detail3"]}
]
显然我的做法是错误的,我正在寻找正确的方法来使用可观察对象执行嵌套请求。
【问题讨论】: