0

I have a problem with my epic, please help me find out where I went wrong. Thank you!

The following code leads me to the error:

const loginEpic = (action$) =>
  action$
    .ofType('LOGIN')
    .switchMap(() => {
      return Observable.fromPromise(loginService())
        .map((result) => {
          return Observable.of({
            payload: result,
            type: types.loginCompleted,
          });
        })
        .catch((error) => {
          return Observable.of({
            payload: error,
            type: types.loginFailed,
          });
        });
    });

And here is my configureStore file:

const epicMiddleware = createEpicMiddleware(rootEpic);

// Ref: https://redux-observable.js.org/docs/recipes/HotModuleReplacement.html
if (module.hot) {
  module.hot.accept('./epic', () => {
    const nextEpic = require('./epic');
    epicMiddleware.replaceEpic(nextEpic);
  });
}

const configureStore = (): Store<any> => {

  const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(epicMiddleware)));
  if (module.hot) {
    module.hot.accept('./reducer', () => {
      const nextReducer = require('./reducer').default;
      store.replaceReducer(nextReducer);
    });

    return store;
  }
};

1 Answer 1

3

I believe you must drop the Observable.of(...) from within the map() (EDIT - thanks to paulpdaniels: but not the catch()) method, because in this way you are returning an observable of observables - see the simplified code below:

Observable.fromPromise(...)
    .map(result => Observable.of(...))  // maps an object to an observable

The entire code should be:

const loginEpic = (action$) =>
  action$
    .ofType('LOGIN')
    .switchMap(() => {
      return Observable.fromPromise(loginService())
        .map((result) => {
          return {
            payload: result,
            type: types.loginCompleted,
          };
        })
        .catch((error) => {
          return Observable.of({
            payload: error,
            type: types.loginFailed,
          });
        });
    });
Sign up to request clarification or add additional context in comments.

5 Comments

You overshot, correct to remove from map, but catch requires an Observable return.
@paulpdaniels Yes, absolutely right, thank you for the correction!
Oh I didn't recognize it. Thank you ;)
@paulpdaniels yes, in my case I fixed it like this:.catch((err: any, caught: Observable<any>) => { return Observable.of({ payload: err, type: types.loginFailed, }); }));
not working for me. I am still getting the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.