如何使用forkjoin返回两个独立API呼叫的合并响应

发布于 2025-01-22 10:32:04 字数 1599 浏览 2 评论 0原文

我有两个独立的API电话。以下是两个API调用的代码:

public getLendingType(partyId: string): Observable<{ lendingType: string, partyId: string }> {
    return this.customerRepositoryService.getCustomer(partyId).pipe(
        map((customer: ICustomer) => {
          return { lendingType: customer.LendingType, partyId: partyId } ;
        })
      );
  }

  private getPartyType(partyId: string): Observable<{ partyType: string}> {
    return this.partyRepositoryService.getParty(partyId).pipe(
      map((party: IParty) => {
        return  { partyType: party.PartyType };
      })
    );
  }

您可以看到两个API调用彼此独立。在这一点上,我想使用forkjoin结合他们的响应并返回组合结果。这就是我被卡住的地方。我为此部分编写的代码如下:

  public getPartyTypeAndLendingType(partyId: string): Observable<{ partyType: string, lendingType: string, partyId: string }> {

        return forkJoin([this.getLendingType(partyId), this.getPartyType(partyId)]).subscribe ( result => {
            return { partyType: result[1].partyType, lendingType: result[0].lendingType, partyId: result[0].partyId }
        })
  }

但是我认为我缺少一些内容,但我仍然会遇到编译器错误,如下图:

“在此处输入映像说明”

,并在编译器错误上悬停在下面显示的消息:

Type 'Subscription' is missing the following properties from type 'Observable<{ partyType: string; lendingType: string; partyId: string; }>': source, operator, lift, subscribe, and 3 more.ts(2740

如何实现这一目标并使此代码正常工作?

I have two independent api calls. Below is the code for two api calls:

public getLendingType(partyId: string): Observable<{ lendingType: string, partyId: string }> {
    return this.customerRepositoryService.getCustomer(partyId).pipe(
        map((customer: ICustomer) => {
          return { lendingType: customer.LendingType, partyId: partyId } ;
        })
      );
  }

  private getPartyType(partyId: string): Observable<{ partyType: string}> {
    return this.partyRepositoryService.getParty(partyId).pipe(
      map((party: IParty) => {
        return  { partyType: party.PartyType };
      })
    );
  }

As you can see both api call is independent from each other. At this point I want to use forkjoin to combine their responses and return the combined result. This is where I got stuck. The code I have written for this part is below:

  public getPartyTypeAndLendingType(partyId: string): Observable<{ partyType: string, lendingType: string, partyId: string }> {

        return forkJoin([this.getLendingType(partyId), this.getPartyType(partyId)]).subscribe ( result => {
            return { partyType: result[1].partyType, lendingType: result[0].lendingType, partyId: result[0].partyId }
        })
  }

but I think I am missing something for which still I am getting compiler error like the image below:

enter image description here

and hovering over the compiler error it showing below messages:

Type 'Subscription' is missing the following properties from type 'Observable<{ partyType: string; lendingType: string; partyId: string; }>': source, operator, lift, subscribe, and 3 more.ts(2740

How to achieve this and make this piece of code working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度° 2025-01-29 10:32:04

因为您正在调用.subscribe()您的功能返回sisscription而不是可观察的

要转换API调用的结果,请使用.pipe()使用map()。这样,您的功能将返回可观察的。

return forkJoin([
    this.getLendingType(partyId), 
    this.getPartyType(partyId)
]).pipe(
  map(result => 
    ({ partyType: result[1].partyType, lendingType: result[0].lendingType, partyId: result[0].partyId })     
  )
)

Because you are calling .subscribe() your function returns a Subscription instead of an Observable.

To transform the result of the API calls use a .pipe() with map(). This way your function will return an Observable.

return forkJoin([
    this.getLendingType(partyId), 
    this.getPartyType(partyId)
]).pipe(
  map(result => 
    ({ partyType: result[1].partyType, lendingType: result[0].lendingType, partyId: result[0].partyId })     
  )
)
寄与心 2025-01-29 10:32:04

您遇到了错误,因为在方法签名上,您说的是您将返回可观察的,但是您要返回的是subpcription。有一个简单的修复程序,但是您真的很接近:),

public getPartyTypeAndLendingType(partyId: string): Observable<{ partyType: string, lendingType: string, partyId: string }> {
  const lendingType = this.getLendingType(partyId);
  const partyType = this.getPartyType(partyId);

  return forkJoin([lendingType, partyType]).pipe(
    map(result => ({
      partyType: result[1].partyType,
      lendingType: result[0].lendingType,
      partyId: result[0].partyId
    }))
  );
}

而不是在该方法中订阅(这是对某些目的,实际上取决于您的需求),我们现在使用MAP运算符来映射反应。现在订阅部分应由呼叫者完成。

You are having an error because at the method signature, you are saying that you will return an Observable but what you are returning instead is a Subscription. There is an easy fix, you were really close though :)

public getPartyTypeAndLendingType(partyId: string): Observable<{ partyType: string, lendingType: string, partyId: string }> {
  const lendingType = this.getLendingType(partyId);
  const partyType = this.getPartyType(partyId);

  return forkJoin([lendingType, partyType]).pipe(
    map(result => ({
      partyType: result[1].partyType,
      lendingType: result[0].lendingType,
      partyId: result[0].partyId
    }))
  );
}

Instead of subscribing within the method (which is fine for some purposes, really depends on your needs), we are now using the map operator to map the response. Now the subscription part, should be done by the caller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文