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")
})
};