Angular 5嵌套的HTTP客户端可观察对象返回对象 [英] Angular 5 nested http client observables return object

查看:72
本文介绍了Angular 5嵌套的HTTP客户端可观察对象返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个可观察到的响应,在其中我进行一个http调用,然后在返回响应之前,我进行了另一个http调用以填充第一个调用的返回对象,如下所示.

I am trying to create an observable response where I make one http call and then before returning the response, I make another http call to populate the return object of the first call as shown below.

getOrderWithItems(orderId: string, includes: Set<OrderInclude>): Observable<OrderDto> {
    return this.getOrder(orderId, includes)
      .map(order => {
        this.searchItems().subscribe(
          items => {
            order.items = items.results;
            return order;
          }
        )
      });
  }

编译器给出错误:无法将类型可观察"分配给类型可观察".不知道如何使它工作. this.getOrder()和this.searchItems()都映射http调用,这些调用返回相应的可观察对象.

The compiler gives an error: Type 'Observable' is not assignable to type 'Observable'. Not sure how to get this to work. this.getOrder() and this.searchItems() both map http calls which return corresponding observables.

推荐答案

您的地图不返回任何内容.它只订阅另一个Observable.

Your map doesn't return anything. It only subscribes to another Observable.

要将一个Observable转换为另一个Observable(将 getOrder 转换为 searchItems ),请使用平面运算符之一.就您而言, switchMap 将适合:

For transforming one Observable into another (getOrder to searchItems), use one of the flat Operators. In your case, switchMap would fit:

this.getOrder(orderId, includes)
  .switchMap(order => 
    this.searchItems()
      .map(items => ({...order, items: items.results}))
  );

这篇关于Angular 5嵌套的HTTP客户端可观察对象返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆