@ngrx Effect does not run the second time

北城余情 提交于 2019-11-28 12:10:02

It sounds like an error is occurring. In that situation, the action in the observable returned by catch will be emitted into the effect's stream and the effect will then complete - which will prevent the effect from running after the error action is emitted.

Move the map and the catch into the switchMap:

@Effect() event_response$ = this.action$
  .ofType(SEND_EVENT_RESPONSE_ACTION)
  .map(toPayload)
  .switchMap((payload) => this.myService
    .eventResponse(payload.eventId, payload.response)
    .map(data => new SentEventResponseAction(data))
    .catch((error) => Observable.of(new ErrorOccurredAction(error)))
);

Composing the catch within the switchMap will prevent the effect from completing if an error occurs.

You must move map() and catchError() into swithchMap() as following

@Effect()
public event_response$ = this.action$.pipe(
    ofType(SEND_EVENT_RESPONSE_ACTION),
    switchMap((payload) => {
        return this.myService.eventResponse(payload.eventId,payload.response).pipe(
            map((data: DataType) => new SentEventResponseAction(data)),
            catchError((error) => Observable.of(new ErrorOccurredAction(error)))
        })
    );
 );

Please note that, evetResponse() method inside myService should return an observable in order to use pipe afterward. In case your method inside service returns Promise, you can convert it into an observable by the use of from in the rxjs package as below:

import { from } from 'rxjs';
...
const promise = this.myService.eventResponse(payload.eventId,payload.response);
const observable = from(promise);
return observable.pipe(...

For more and detail description take a look at this link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!