我在我的传奇中调用一个 firebase 函数。该函数工作正常,如果返回错误,则会进入 saga 的 catch block 。但错误变量没有传递。代码如下:
const firebaseAuth = ({ email, password }) =>
firebase.auth().signInWithEmailAndPassword(email, password)
.catch((error) => { throw error; });
function* loginUser(action) {
try {
const user = yield call(firebaseAuth, action.payload);
yield put({ type: USER_LOGIN_SUCCESS, payload: { user } });
} catch (error) {
/* eslint-disable no-debugger */
debugger;
// I need to see the error here.
yield put({ type: USER_LOGIN_FAIL });
}
}
function* sessionSaga() {
yield takeEvery(USER_LOGGING_IN, loginUser);
}
export default sessionSaga;
目前,当程序在调试器
语句处中断时,错误未定义。知道如何解决这个问题吗?
最佳答案
我认为你应该在 try-catch block 中添加调用 Promise 的函数:
function* loginUser(action) {
try {
const firebaseAuth = ({ email, password }) =>
firebase.auth().signInWithEmailAndPassword(email, password)
.catch((error) => { throw error; });
const user = yield call(firebaseAuth, action.payload);
yield put({ type: USER_LOGIN_SUCCESS, payload: { user } });
} catch (error) {
/* eslint-disable no-debugger */
debugger;
// I need to see the error here.
yield put({ type: USER_LOGIN_FAIL });
}
}
关于javascript - 如何将 promise 中的错误返回到 redux saga catch block ?现在得到错误未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45672144/