【问题标题】:You provided `undefined` where a stream was expected in Redux Observable您在 Redux Observable 中预期流的位置提供了 `undefined`
【发布时间】:2020-06-15 15:22:25
【问题描述】:

试图围绕 RxJS 和 redux observable 进行思考

我有这个:

export const getSomeData = (action$) =>
    action$.pipe(
        ofType(GET_USER_DATA),
        mergeMap((action) => getData(action.id)),
        map(fetchUserFulfilled),
        catchError((e) =>
            of({
                type: 'FAILED_TO_FETCH_DATA',
                e,
            }),
        ),
    )

效果很好,但现在我想在取回数据时实际触发 2 个 observables,所以我尝试了这个:

export const getSomeData = (action$) =>
    action$.pipe(
        ofType(GET_USER_DATA),
        mergeMap((action) => getData(action.id)),
        mergeMap((data) => {
            const newData = data.somethingelse
            of(fetchUserFulfilled(newData), anotherOne(newData))
        }),

        catchError((e) =>
            of({
                type: 'FAILED_TO_FETCH_DATA',
                e,
            }),
        ),
    )

但这失败了。那么我该如何解决这个问题,我有什么误解,我应该如何正确使用mergeMap

【问题讨论】:

    标签: javascript reactjs rxjs redux-observable


    【解决方案1】:

    mergeMap 应该返回一个 observable

    mergeMap((data) => {
                const newData = data.somethingelse
                return of(fetchUserFulfilled(newData), anotherOne(newData))
            }),
    

    【讨论】:

    • 啊,这几乎是对的,只是没有“返回”任何东西?
    • 如果你不指定返回值,它默认为undefined,现在我不知道内部是如何处理的
    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 2020-03-10
    • 2020-06-16
    • 2018-09-07
    • 2017-09-18
    • 1970-01-01
    • 2023-03-23
    • 2021-07-14
    相关资源
    最近更新 更多