您在 Redux Observable 中预期流的位置提供了 `undefined`

IT技术 javascript reactjs rxjs redux-observable
2022-08-04 00:53:50

试图围绕 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正确使用?

1个回答

mergeMap应该返回一个可观察的

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