admin管理员组文章数量:1346053
I need know a good practise for this problem.
I am calling to three services concat with switchMaps, but I need a variable of first service for the last service. How was the best practice to do this?
Example:
this.session.account.get()
.switchMap(account => this.employerApi.get(account.accountId))
.switchMap(employer => this.addressApi.get(employer.addressId))
.filter(address => address.numer % 2)
.subscribe(address => console.log(¿¿¿¿¿account.name?????, address.name));
Thanks for your help
I need know a good practise for this problem.
I am calling to three services concat with switchMaps, but I need a variable of first service for the last service. How was the best practice to do this?
Example:
this.session.account.get()
.switchMap(account => this.employerApi.get(account.accountId))
.switchMap(employer => this.addressApi.get(employer.addressId))
.filter(address => address.numer % 2)
.subscribe(address => console.log(¿¿¿¿¿account.name?????, address.name));
Thanks for your help
Share Improve this question asked Jun 27, 2018 at 12:47 srhuevosrhuevo 4203 silver badges18 bronze badges2 Answers
Reset to default 12The simplest way would be to aggregate the values as you go:
this.session.account.get()
.switchMap(account =>
this.employerApi.get(account.accountId).map(employer => ({employer, account}))
.switchMap(data =>
this.addressApi.get(employer.addressId).map(address => ({...data, address}))
.filter(data => data.address.number % 2)
.subscribe(...)
Not sure this is the best way to do it, but here is a way to do it :
this.session.account.get()
.switchMap(account => zip(Observable.from(account), this.employerApi.get(account.accountId))
.switchMap((account, employer) => zip(Observable.from(account), this.addressApi.get(employer.addressId))
.filter((account,address) => address.numer % 2)
.subscribe((account,address) => console.log(¿¿¿¿¿account.name?????, address.name));
In terms of best practices however, I think what you need is a serializer in the backend that returns the results in a json format, something like : { "account": account_object, "address" : address_object}
.
本文标签: javascriptrxjs switchmap observablesave variables for finalStack Overflow
版权声明:本文标题:javascript - rxjs switchmap observable, save variables for final - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743821673a2544899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论