我有这个史诗:
export const fetchProductsFulfilledEpic = action$ =>
action$.ofType(FETCH_PRODUCTS_FULFILLED)
.mergeMap(action => {
return Observable.of(
updateSearchResults(action.payload),
toggleMenu(),
updateRegion(action.payload)
).catch(error => Observable.of(
fetchProductsRejected(error))
)
})
我想只返回
updateSearchResults(action.payload),
toggleMenu(),
updateRegion(action.payload)
如果action.payload.products.length
大于0,否则显示警告。
Attmept:
export const fetchProductsFulfilledEpic = action$ =>
action$.ofType(FETCH_PRODUCTS_FULFILLED)
.mergeMap(action => {
Observable.if(() => action.payload.products.length > 0,
Observable.of(
updateSearchResults(action.payload),
toggleMenu(),
updateRegion(action.payload)
), Observable.of(...enter alert observable here...)
).catch(error => Observable.of(
fetchProductsRejected(error))
)
})
收到错误:
您提供了“未定义”的预期流。你可以提供 一个Observable,Promise,Array或Iterable。
如何返回上面的Observable.of
,如果showAlertDialog
,则返回action.payload.products.length < 1
的观察点。
答案 0 :(得分:2)
mergeMap
内的正常条件怎么样?
export const fetchProductsFulfilledEpic = action$ =>
action$.ofType(FETCH_PRODUCTS_FULFILLED)
.mergeMap(action => {
if (action.payload.products.length > 0) {
return Observable.of(
updateSearchResults(action.payload),
toggleMenu(),
updateRegion(action.payload)
);
} else {
return Observable.of(
showAlertDialog()
);
}
})
作为一个侧面点,您的catch
目前无关紧要,因为Observable.of
永远不会抛出错误:
.mergeMap(action => {
return Observable.of(
updateSearchResults(action.payload),
toggleMenu(),
updateRegion(action.payload)
// this is catching errors on the above Observable but
// this Observable will never ever throw errors
).catch(error => Observable.of(
fetchProductsRejected(error)
))
})