我面临以下情况。有一个前端应用程序,构建与角(2),这是连接的休息后端可通过http访问。一旦用户登录(使用电子邮件和pwd),后端将返回与用户相关的数据,特别是与用户关联的id (让我们调用此userId)和他所在区域的代码(让我们称之为regionId)。通过userId,我可以查询后端以获得用户所属的关联组ID (让我们称之为affinityId)。最后,使用affinityId和regionId,我可以从后端检索折扣,以应用于用户。
换句话说,后端提供了3个API:
如果我通过角http客户端访问这些API,我可以构建以下API(类似于Javascript伪代码)
我想知道,我是否可以结合使用RxJs操作符,以获得折扣信息,我正在寻找这样的观测数据。
我可以通过switchMap连接前两个APIs以获得affinityId,如下所示
this.getUserData(email, pwd)
.switchMap(userData => this.getAffinityPerUser(userData.userId))但是,我不知道如何将affinityId可观测(由getAffinityPerUser返回)与userData可观测(由getUserData返回)结合起来,以便能够同时拥有affinityId和regionId,这是我需要调用的getDiscount API。
任何帮助都是非常感谢的
发布于 2017-01-17 16:49:20
您可以像这样将所有操作组合在一起:
this.getUserData(email, pwd)
.switchMap(userData => this.getAffinityPerUser(userData.userId)
.map(affinity => [affinity, userData.regionId])) // return a tuple combining required elements
.switchMap(([affinity, regiondId) => this.getDiscount(regionId, affinity)) // use destructuration to extract the 2 elements of the tuplehttps://stackoverflow.com/questions/41702140
复制相似问题