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

标签 javascript redux redux-saga

我在我的传奇中调用一个 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/

相关文章:

javascript - 谷歌分析 : External . js 文件

javascript - Vaadin:通过 "close"、 "ok"或 "x"关闭通知?

javascript - 使用 Google Visualization,为什么 DataView 内容显示在 ChartRangeFilter 中,而不显示在其关联的 LineChart 中?

javascript - 在 Flux 架构中,如何管理分析跟踪?

javascript - React-Redux,调度函数时, "uncaught TypeError is not a function"

javascript - 将 Ruby 代码移植到 JavaScript 时出现 "Call stack size exceeded"

javascript - 当从 cookie 异步加载身份验证 token 时,React React-router-dom 私有(private)路由不起作用

javascript - 与 Redux react : error on propType property

reactjs - 顺序调用 redux sagas

javascript - redux-saga 使用正则表达式