Rxjs 使用链早期阶段的先前可观察值
如何使用链早期阶段(不是最后阶段)的可观察值。
我有这样的代码:
deleteGroceryStoreAndItsOwner() {
this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
.flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries.filter(groceryStoreOwnerSummary=> groceryStoreOwnerSummary.user.gender == "Male"))
.flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid))
.flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
.flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
.flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)) // here is where i have the problem: how can i use the observable from early stage of the chain?
.flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
.subscribe(() => {
console.log("deleted grocery store")
console.log("deleted grocery store owner")
})
};
在删除第四个平面图中的杂货店后,我需要删除杂货店老板,这是来自第二个平面图的。
注意:
- 每个删除方法(deleteGroceryStore和deleteGroceryStoreOnwer)都会返回一个Observable。
- 我的应用程序的角度版本不支持管道,因此请不要使用 .pipe 给出答案。
更新:2022 年 3 月 10 日 我像这样修改了我的代码,但由于某种原因,地图中的代码没有被执行,知道为什么吗?
deleteGroceryStoreAndItsOwner() {
this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
.flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
.filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male"))
.map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
.flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
.flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
.flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
})
.subscribe(() => {
console.log("deleted grocery store")
console.log("deleted grocery store owner")
})
};
我也尝试过这个,但仍然不起作用:
deleteGroceryStoreAndItsOwner() {
this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
.flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
.filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male")
.map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
.flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
.flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
.flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
}))
.subscribe(() => {
console.log("deleted grocery store")
console.log("deleted grocery store owner")
})
};
How do I use an observable from early stage of the chain (not the last one).
I have code like this:
deleteGroceryStoreAndItsOwner() {
this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
.flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries.filter(groceryStoreOwnerSummary=> groceryStoreOwnerSummary.user.gender == "Male"))
.flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid))
.flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
.flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
.flatMap((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)) // here is where i have the problem: how can i use the observable from early stage of the chain?
.flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
.subscribe(() => {
console.log("deleted grocery store")
console.log("deleted grocery store owner")
})
};
after I deleted the grocery store in the 4th flat map, I need to delete the grocery store owner, and this is from the second flatmap.
notes:
- each delete method (deleteGroceryStore and deleteGroceryStoreOnwer) returns an Observable.
- My app's angular version does not support pipe, so please don't give answers with .pipe.
Update: March 10 2022
I modified my code like this but for some reason the code in the map is not getting executed, any idea why?
deleteGroceryStoreAndItsOwner() {
this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
.flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
.filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male"))
.map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
.flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
.flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
.flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
})
.subscribe(() => {
console.log("deleted grocery store")
console.log("deleted grocery store owner")
})
};
I also tried this but that still did not work:
deleteGroceryStoreAndItsOwner() {
this.groceryStoreOwnerService.getGroceryStoreOwnerSummaries(this.city.uuid)
.flatMap((groceryStoreOwnerSummaries: GroceryStoreOwnerSummary[]) => aircraftUserSummaries
.filter(groceryStoreOwnerSummary => groceryStoreOwnerSummary.user.gender == "Male")
.map((groceryStoreOwnerSummary: GroceryStoreOwnerSummary) => {
this.groceryStoreService.searchGroceryStore(this.city.uuid, groceryStoreOwnerSummary.user.uuid)
.flatMap((groceryStoreSummary: GroceryStoreSummary) => this.groceryStoreService.getGroceryStore(groceryStoreSummary.uuid))
.flatMap((groceryStore: GroceryStore) => this.groceryStoreService.deleteGroceryStore(groceryStore))
this.groceryStoreOwnerService.getGroceryStoreOwner(groceryStoreOwnerSummary)
.flatMap((groceryStoreOwner: GroceryStoreOwner) => this.groceryStoreOwnerService.deleteGroceryStoreOnwer(groceryStoreOwner))
}))
.subscribe(() => {
console.log("deleted grocery store")
console.log("deleted grocery store owner")
})
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过嵌套运算符而不是链接它们来实现此目的。
这是一个不起作用的简化示例:
当我们嵌套运算符时,所有参数都可以在
map
内访问:FWIW,这里有一些 StackBlitz
You can accomplish this by nesting the operators instead of chaining them.
Here's a simplified example that WON'T work:
When we nest the operators, all the parameters are accessible inside the
map
:FWIW, here's a little StackBlitz