redux-observable 操作必须是普通对象。使用自定义中间件进行异步操作

redux-observable Actions must be plain objects. Use custom middleware for async actions

当我在下面的代码中注释掉 getCurrentPositionEpic 时,该应用程序可以运行。但如果我不加注释,我会收到错误消息:

Actions must be plain objects. Use custom middleware for async actions.

export const rootEpic = combineEpics(
  fetchCategoriesEpic,
  getCurrentLocationEpic,
  getCurrentPositionEpic
)

const store = createStore(
  rootReducer,
  initialState,
  composeWithDevTools(
    applyMiddleware(createEpicMiddleware(rootEpic))
  )
)

location.epic.js

const getCurrentPosition$ = getCurrentPositionObservable(
  { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
)

getCurrentPosition$.subscribe(
  (position) => {
    console.log(position)
    const positionObject = {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    }
    //store.dispatch(updateRegion(positionObject))
    //getCurrentLocation(positionObject)
  },
  (err) => {
    console.log('Error: %s', err)
  },
  () => {
    console.log('Completed')
  })

export const getCurrentLocationEpic = action$ =>
  action$.ofType(GET_CURRENT_LOCATION)
    .mergeMap(() =>
      Observable.fromPromise(Geocoder.geocodePosition(makeSelectLocation()))
        .flatMap((response) => Observable.of(
          getCurrentLocationFulfilled(response)
        ))
        .catch(error => Observable.of(getCurrentLocationRejected(error)))
    )

export const getCurrentPositionEpic = action$ =>
  action$.ofType(GET_CURRENT_POSITION)
    .mapTo(() => getCurrentPosition$
      .flatMap((response) => Observable.of(
        getCurrentPositionFulfilled(response)
      ))
      .catch(error => Observable.of(getCurrentLocationRejected(error)))
    )

下面的代码只是一个帮助程序,用于将 React Native navigator.geolocation.getCurrentPosition 转换为可观察对象,而不是接受回调的函数。

callBackToObservable.js

import { Observable } from 'rxjs'

export const getCurrentPositionObservable = Observable.bindCallback(
  (options, cb) => {
    if (typeof options === 'function') {
      cb = options
      options = null
    }
    navigator.geolocation.getCurrentPosition(cb, null, options)
  })

可能导致错误的原因是什么?

尝试路过商店:

export const getCurrentPositionFulfilledEpic = (action$, store) =>
  action$.ofType(GET_CURRENT_POSITION_FULFILLED)
        .mergeMap(() =>{
  console.log(store)***************** store is populated here
  return Observable.fromPromise(Geocoder.geocodePosition({
    lat: store.getState().get('searchForm').get('position').lat,***but not here
    lng: store.getState().get('searchForm').get('position').lng
  }))
    .flatMap((response) => Observable.of(
      getCurrentLocationFulfilled(response)
    ))
    .catch(error => Observable.of(getCurrentLocationRejected(error)))
}
)

Geocoder.geocodePosition

使用https://github.com/devfd/react-native-geocoder

问题是您对 mapTo 的使用。你基本上是在说 "map this action to an Observable" 所以现在你的史诗 returns 一个 Observable of Observable of actions Observable<Observable<Action>> 而不仅仅是一个 Observable of actions.

换句话说,您的史诗现在发出 Observables 而不是发出动作。相反,您需要使用 mergeMapswitchMap 等合并策略运算符将内部 Observable 链 flatten/merge 合并到 top-level 链中。 flatMapmergeMap 的别名,顺便说一句。

export const getCurrentPositionEpic = action$ =>
  action$.ofType(GET_CURRENT_POSITION)
    .mergeMap(() => getCurrentPosition$
      .flatMap((response) => Observable.of(
        getCurrentPositionFulfilled(response)
      ))
      .catch(error => Observable.of(getCurrentLocationRejected(error)))
    )

另一件事——您不需要使用 flatMap 又名 mergeMapgetCurrentPosition$ 映射到 getCurrentPositionFulfilled 操作,因为它是 1:1.如果它是 1 对多,你只需要它。

export const getCurrentPositionEpic = action$ =>
  action$.ofType(GET_CURRENT_POSITION)
    .mergeMap(() => getCurrentPosition$
      .map((response) => getCurrentPositionFulfilled(response))
      .catch(error => Observable.of(getCurrentLocationRejected(error)))
    )

按您的方式使用它并没有真正的危害,但它可能会使以后维护代码的其他人感到困惑。