Add data to http response using rxjs

孤街醉人 提交于 2019-12-06 01:32:48
Thierry Templier

You could use the flatMap operator and Observable.forkJoin as described below:

this.http.get('/trips/someid')
   .map(res => res.json())
   .flatMap(res => {
     return Observable.forkJoin([
       Observable.of(res),
       this.http.get('/drivers/'+res['driver_id']).map(res => res.json())
     ]);
   })
   .map(res => {
     res[0]['detailed_driver'] = res[1];
     return res[0]
   })
 .subscribe(
   (data) => {
   }
 );

The flatMap allows to execute another request when the first one is received. Observable.forkJoin allows to receive both the response of the first response and the result of the second one at the end.

This way you will be able to update the first result with the second one...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!