如何将promise中的错误返回到redux saga catch块?现在得到错误未定义

时间:2017-08-14 10:08:21

标签: javascript redux redux-saga

我在我的传奇中调用了firebase函数。该函数正常工作,如果错误返回,它将进入saga的catch块。但是没有传递错误变量。这是代码:

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;

目前,当程序在debugger语句处中断时,错误未定义。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

Ι认为你应该在try-catch块中添加调用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 });
  }
}