简体   繁体   English

嵌套 angular http 可观察调用

[英]Nested angular http observable calls

Been stuck on this problem for a little while now.一直在这个问题上停留了一段时间。 What I am trying to achieve is to swap out the product ids with the http response from an api.我想要实现的是用来自 api 的 http 响应替换产品 ID。

The appConfig response (simplified) appConfig 响应(简化)

[
  {
    ...,
    hotspot: [
      {
        ...,
        data: {
          ...,
          products: [1234, 5678]
        }
      },
      {
        ...,
        data: {
          ...,
          products: [8910, 1112]
        }
      }      
    ]
  }
]

The code that I currently am using (really basic for now).我目前正在使用的代码(现在真的很基础)。

public readonly loadAppConfig$ = this._retailerSrv.retailer$.pipe(
    filter((retailer) => Boolean(retailer)),
    switchMap((retailer) =>
      combineLatest([
        of(retailer),
        this._roomConfigSrv.loadAppConfig$(),
        this._authSrv.getApiKey$(retailer),
      ])
    ),
    map(([retailer, appConfigs, authResponse]) => {
      return appConfigs.map((appConfig) => {
        this._authSrv.apiKey = authResponse.access_token;
        return {
          ...appConfig,
          hotspots: appConfig.hotspots.map((hotspotConfig) => {
            if (
              hotspotConfig.type === 'products' &&
              hotspotConfig.data.products
            ) {
              return {
                ...hotspotConfig,
                data: hotspotConfig.data.products.map((productId) =>
                  this._authSrv.getProductFromApi$(
                    productId,
                    retailer
                  )
                ),
              };
            }
            return {
              ...hotspotConfig,
            };
          }),
        };
      });
    }),
    tap(console.log)
  );

Which basically returns me with http observables in place of the product ids (not api responses).这基本上用 http 可观察值代替产品 ID(不是 api 响应)返回给我。

Result from tap点击结果

[
  {
    ...,
    hotspot: [
      {
        ...,
        data: {
          ...,
          products: [Observable, Observable]
        }
      },
      {
        ...,
        data: {
          ...,
          products: [Observable, Observable]
        }
      }      
    ]
  }
]

I know there would be a simpler way to achieve this but I haven't found anything so far (tried promises etc).我知道会有一种更简单的方法来实现这一点,但到目前为止我还没有找到任何东西(尝试过的承诺等)。 Any help would be much appreciated, and please let me know if you have any other questions.任何帮助将不胜感激,如果您有任何其他问题,请告诉我。

Cheers干杯

your code don't give any clue about your.json and what you want get it, but the idea it's always the same您的代码没有提供有关 your.json 以及您想要得到的任何线索,但想法始终相同

  1. you make a call to get an array你打电话来获取一个数组
  2. You create an array with elements you need search您创建一个包含您需要搜索的元素的数组
  3. you make a forkJoin of this new elements你制作了这个新元素的 forkJoin
  4. you add the result to the first array您将结果添加到第一个数组

In pseudo code, use as "inspiration"在伪代码中,用作“灵感”

loadAppConfig.pipe(
switchMap(configData=>{
    ..here we has config..
    let products=[]; //we create an array with the products
    configData.hotspot.forEach(x=>{
       products=[...products,x.data.products] //we fill the array "products" 
    })
    return forkJoin(products.map(p=>loadProduct(p)).pipe( //we create an array of observables
        map(productResult=>{
            ..here change the "config" that is store in "configData"...
            ..using "productResult"...,e.g.
            configData.hotsport.forEach(x=>{
              const product=productResult.find(x=>p.key==x.data.product
               x.data.productData=product
            })
            ...finally...
            return configData
        }
    )  
}
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM