0

我的史诗有问题,请帮我找出哪里出错了。谢谢!

以下代码导致我出现错误:

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

这是我的 configureStore 文件:

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

4

1 回答 1

3

我相信您必须Observable.of(...)map()( EDIT - 感谢 paulpdaniels:但不是catch()) 方法中删除,因为这样您将返回一个可观察的可观察对象 - 请参见下面的简化代码:

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

整个代码应该是:

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,
          });
        });
    });
于 2017-05-31T13:31:30.680 回答