Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular RXJS多个HTTP嵌套请求_Angular_Rxjs_Angular2 Observables_Fork Join - Fatal编程技术网

Angular RXJS多个HTTP嵌套请求

Angular RXJS多个HTTP嵌套请求,angular,rxjs,angular2-observables,fork-join,Angular,Rxjs,Angular2 Observables,Fork Join,我可以使用forkjoin操作符成功地进行多个并行http调用(示例call all sales data),但是我需要根据每个第一个(sales)http调用的结果连接进一步的http调用。我不确定mergeMap或concat操作符是否最好,以及它们如何适合这里 示例数据是 销售 {saleId:100,name:'bob,uri:'http://testserver/sales/100'} {saleId:101,姓名:'fred,uri:'http://testserver/sales/

我可以使用forkjoin操作符成功地进行多个并行http调用(示例call all sales data),但是我需要根据每个第一个(sales)http调用的结果连接进一步的http调用。我不确定mergeMap或concat操作符是否最好,以及它们如何适合这里

示例数据是 销售

{saleId:100,name:'bob,uri:'http://testserver/sales/100'}
{saleId:101,姓名:'fred,uri:'http://testserver/sales/101'}
{saleId:102,姓名:'billy,uri:'http://testserver/sales/102'}
产品
{id:1,saleId:101,细节:'test1',产品:{id:200,uri:'http://testserver/product/200'} }
{id:1,saleId:101,细节:'test1',产品:{id:201,uri:'http://testserver/product/201'} }
{id:1,saleId:101,细节:'test1',产品:{id:202,uri:'http://testserver/product/202'} }
getServerChildNodes(childNodeObj:any[]):可观察{
//ObservableListArray包含http销售uri
返回forkJoin(observablesListArray)
.map((数据:any[])=>{
for(设i=0;i
我不确定我是否正确理解了您的问题,但似乎您正在寻找一种方法来连接两个后续http调用,第二个调用需要第一个调用返回一些数据。另外,您希望并行运行许多这样的链式调用

如果是这样的话,这可能是一种方法

getSaleIds(): Observable<string[]>;

getSale(id: string): Observable<Sale> {
  // calls and http service and returns a Sale for a certain id
  // each Sale contains a productId to be used in the subsequent call
}

getProduct(productId: string): Observable<Product> {
  ...
}

getSaleAndThenProduct(saleId: string) {
  return getSale(saleId).pipe(
    switchMap(sale => getProduct(sale.productId),
    map(product => ({sale, product}))
  );
}

getSaleIds()
.pipe(
  map(saleIds => saleIds.map(id => getSaleAndThenProduct(id)),
  switchMap(functions => forkJoin(functions))
)
.subscribe(salesAndProducts => // do something)
getSaleIds():可观察;
getSale(id:string):可观察{
//调用和http服务并返回特定id的销售
//每次销售都包含一个productId,将在后续调用中使用
}
getProduct(productId:string):可观察{
...
}
GetSaleanThenProduct(saleId:string){
返回getSale(saleId)。管道(
switchMap(sale=>getProduct(sale.productId),
地图(产品=>({sale,product}))
);
}
getSaleIds()
.烟斗(
map(saleIds=>saleIds.map(id=>getSaleanthenProduct(id)),
开关映射(函数=>forkJoin(函数))
)
.subscribe(salesAndProducts=>//做点什么)

salesAndProducts
是类型为
{sale:sale,product:product}的对象数组

谢谢Picci…在您的回复中,salesId设置在CONST saleId中…因此假设它已经可用,但是我的所有销售id/销售数据列表最初是从另一个http服务返回的。第一个http调用是否可以包装到相同的逻辑中,或者是否需要先请求/可用。关于Max
getSaleIds(): Observable<string[]>;

getSale(id: string): Observable<Sale> {
  // calls and http service and returns a Sale for a certain id
  // each Sale contains a productId to be used in the subsequent call
}

getProduct(productId: string): Observable<Product> {
  ...
}

getSaleAndThenProduct(saleId: string) {
  return getSale(saleId).pipe(
    switchMap(sale => getProduct(sale.productId),
    map(product => ({sale, product}))
  );
}

getSaleIds()
.pipe(
  map(saleIds => saleIds.map(id => getSaleAndThenProduct(id)),
  switchMap(functions => forkJoin(functions))
)
.subscribe(salesAndProducts => // do something)