【问题标题】:Redux observable epic with Rxjs issue带有 Rxjs 问题的 Redux 可观察史诗
【发布时间】:2019-12-10 05:40:39
【问题描述】:

我的史诗如下

export const fetchSomethingEpic = (action$, state$) => {
  return action$.pipe(ofType(someActions.FETCH_LEADS),
    mergeMap(action =>
      from(api().get('SOMETHING'))
        .map(response => fetchSomethingSuccess(response.data))
        .catch(error => Observable.of(fetchSomethingError(error))))
  );
}

这里的 api 是 axios.create,它发生在不同的文件中

基本上,当我安装了rxjs-compat 时,这一切正常。但试图删除它,现在给我以下错误

TypeError: Object(...)(...).map is not a function

安装了rxjs-compat,它的工作原理

我有

import { Observable } from 'rxjs/Rx';
import { mergeMap } from 'rxjs/operators';

  "rxjs": "6.2.1",
  "rxjs-compat":"6.2.1"

现在我有

  import { Observable } from 'rxjs'
  import { mergeMap, map} from 'rxjs/operators';

 "rxjs": "^6.5.2"

我在这里错过了什么

【问题讨论】:

    标签: javascript reactjs react-redux rxjs redux-observable


    【解决方案1】:

    您必须使用 pipe 来传递 observable 以使用 map 运算符。同时导入catchError 处理错误。

    export const fetchSomethingEpic = (action$, state$) => {
      return action$.pipe(ofType(someActions.FETCH_LEADS),
        mergeMap(action =>
          from(api().get('SOMETHING'))
            .pipe(map(response => fetchSomethingSuccess(response.data)),
            catchError(error => Observable.of(fetchSomethingError(error))))
      );
    }
    

    【讨论】:

    • 嘿,这会产生错误:TypeError: You provided 'function (source) { return source.lift(new MergeMapOperator(project, concurrent)); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    【解决方案2】:

    如下解决

      export const fetchSomethingEpic = (action$) =>
          action$.ofType(someActions.FETCH_LEADS)
            .pipe(mergeMap((action) => from(api().get('SOMETHING'))
              .pipe(map(response => fetchSomethingSuccess(response.data)),
                catchError(error => fetchSomethingError(error))))
            );
    

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2020-06-11
      • 2020-12-30
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      相关资源
      最近更新 更多