我想结合两个http get请求。 它们应该相互执行,第二个作为参数获得第一个结果的一部分。
然后,合并后的请求应返回如下列表: [第一个结果json,第二个结果json]
我已经在stackoverflow上找到了这个:How to combine two similar http requests returning Rx Observables?
使用forkJoin,我可以将两个请求合并在一起。
return forkJoin(
this.spotify.getSpotifyData(spotifyId),
this.youtube.getYoutubeData(PARAMETER HERE)
);
如何将第一个的结果作为第二个的参数?
更新 该问题被标记为How to finalize nested Observables?的重复项,但该解决方案对我不起作用。
这是我更新的代码:
return this.spotify.getSpotifyData(spotifyId).pipe(
switchMap(
spotifyResult => {
return this.youtube.getYoutubeData(spotifyResult["name"]).pipe(
map(
youtubeVideoResult => {
({spotifyResult, youtubeVideoResult})
})
)
}
)
)
然后我这样订阅:(注意:getDataBySpotifyId方法是上面的方法)
getDataBySpotifyId(id).subscribe(
({data,data2}) => {
console.log("data: " + data);
}
)
,出现以下错误: 错误TypeError:无法读取未定义的属性“数据”