angular - Nested http calls inside @effects -
i have , angular2 application running ngrx store effects handling http requests. working fine straightforward simple http requests: request -> success/fail actions ends modifying store status.
what trying first call return me array of persons , each person returned need http call data them.
what achieve persons, fetch data persons , trigger "success" action complete data.
here original effect simple persons request. working fine:
@effect() requestmanagers = this.action$ .oftype(contributionactions.fetch_managers_request) .map(topayload) .switchmap(payload => this.contributionsservice.requestmanagers() .map(res => this.contributionactions.fetchmanagersreceive(res.json())) .catch(err => this.errorhandler.handleerror(err, 'there error retrieving managers', this.contributionactions.fetchmanagersfailed()) ) ); here try http request each manager receive. using forkjoin doing in parallel "details" call n managers first call.
- for second call use topromise() http call
- forkjoin results calls triggered
before return line return this.contributionactions.fetchmanagersreceive(managers); can print variable "managers" , formatted original data plus data "details" call, working intended.
but @ dispatching "recieve" action this.contributionactions.fetchmanagersreceive(managers); following error:
effect "contributioneffects.requestmanagers" dispatched invalid action @effect() requestmanagers = this.action$ .oftype(contributionactions.fetch_managers_request) .map(topayload) .switchmap(payload => this.contributionsservice.requestmanagers() .map(res => { const managers = res.json(); const managerscontributionscallarray = []; managers.map(manager => { const data = { reviewid: manager.review_id } managerscontributionscallarray.push(this.contributionsservice.requestmanagercontributionspromise(data)); }); observable.forkjoin(managerscontributionscallarray).subscribe(results => { (var index = 0; index < managers.length; index++) { managers[index]['contributions'] = results; } return this.contributionactions.fetchmanagersreceive([managers]); }); }) .catch(err => this.errorhandler.handleerror(err, 'there error retrieving managers', this.contributionactions.fetchmanagersfailed()) ) ); this how tried far expected result, idea on how make work appreciated.
i know if there better approach same result looking for, since i'm pretty sure there more efficient , pretty way same.
thanks in advance.
Comments
Post a Comment