2

我正在使用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 observable docs 和其他几个示例,但我无法确定我可能遗漏了什么。以下是我的行动和有问题的史诗。

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')
);
4

1 回答 1

10

您的史诗是一个带有块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:提供的史诗“handleSearchEpic”之一不返回流。仔细检查您是否缺少退货声明!

于 2017-05-14T09:05:14.283 回答