Epic没有在Redux-Observable中返回流

时间:2017-05-13 23:42:56

标签: javascript rxjs redux-observable

我正在使用redux-observable测试一个辅助项目,我反复遇到这个问题:Uncaught TypeError: combineEpics: one of the provided Epics "handleSearchEpic" does not return a stream. Double check you're not missing a return statement!

我在网上引用了redux可观察文档和其他几个例子,但我无法确定我可能缺少的内容。以下是我的行为和有问题的史诗。

export const searchContent = query => {
  return {
    type: SEARCH_CONTENT,
    query
  }
}

const returnSearchContent = searchResults => {
  return function(dispatch) {
    dispatch({
      type: RETURN_SEARCH_CONTENT,
      searchResults
    });
  }
}

// Epics
export const handleSearchEpic = action$ => {
  action$.ofType(SEARCH_CONTENT)
    .mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
    .map(res => returnSearchContent(res))
}

export const rootEpic = combineEpics(
  handleSearchEpic
);

以下是应用程序的根目录和商店配置:

const epicMiddleware = createEpicMiddleware(rootEpic);
const store = createStore(Reducer, applyMiddleware(epicMiddleware));

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

1 个答案:

答案 0 :(得分:4)

你的handleSearchEpic史诗是带有块的箭头函数,但实际上并没有返回该流。

export const handleSearchEpic = action$ => { // <-- start of block
  // vvvvvvv missing return
  action$.ofType(SEARCH_CONTENT)
    .mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
    .map(res => returnSearchContent(res))
} // <-- end of block

export const handleSearchEpic = action$ => {
  return action$.ofType(SEARCH_CONTENT)
    .mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
    .map(res => returnSearchContent(res))
}

隐含回归?

或者,您可以删除块并使用隐式返回,这可能是您的意图吗?

export const handleSearchEpic = action$ => // <--- no block
  action$.ofType(SEARCH_CONTENT)
    .mergeMap(action => axios.get(`...SOME_API_ENDPOINT`))
    .map(res => returnSearchContent(res));

一个非常常见的错误,这就是为什么我添加了您提供的错误消息,但它似乎并没有使解决方案易于理解。有关如何改进错误消息的任何建议吗?

  

combineEpics:提供的一个Epics&#34; handleSearchEpic&#34;不返回流。仔细检查你是否错过了退货声明!