如何使用forkjoin返回两个独立API呼叫的合并响应
我有两个独立的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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您正在调用
.subscribe()
您的功能返回sisscription
而不是可观察的
。要转换API调用的结果,请使用
.pipe()
使用map()
。这样,您的功能将返回可观察的。Because you are calling
.subscribe()
your function returns aSubscription
instead of anObservable
.To transform the result of the API calls use a
.pipe()
withmap()
. This way your function will return an Observable.您遇到了错误,因为在方法签名上,您说的是您将返回
可观察的
,但是您要返回的是subpcription
。有一个简单的修复程序,但是您真的很接近:),而不是在该方法中订阅(这是对某些目的,实际上取决于您的需求),我们现在使用
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 aSubscription
. There is an easy fix, you were really close though :)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.