如何解决错误“操作必须是普通对象。在Redux-Saga中使用自定义中间件进行异步操作

时间:2019-11-05 03:35:58

标签: reactjs redux-saga

我正在使用redux saga实现登录,该登录saga可以正常工作,直到我发布以下错误信息:“操作必须是普通对象。对异步操作使用自定义中间件。

为什么会出现此错误?,一切正常。

我的传奇故事如下:

function* loginWithEmailSaga(payload) {
  const headerParams = {
    'Content-Type': 'application/json'
  };
  const apiCall = () => axios.post(route, {
    username: payload.email,
    password: payload.password
  },
  { headerParams },
  ).then(response => response.data)
    .catch(err => {
      console.log(err);
    });

  try {
    const data = yield call(apiCall);

    console.log(data);
    if (data.success) {
    yield put(loginWithEmailSuccess(data.data));
    yield history.push('/app');

    }
  } catch (error) {
    console.log(error);
    yield put(loginWithEmailFailure(error));
  }
}

我的动作是这样的

export const loginWithEmail = (email, password) => ({
  type: types.LOGIN_WITH_EMAIL_REQUEST,
  email,
  password
});

export const loginWithEmailSuccess = credential => ({
  type: types.LOGIN_WITH_EMAIL_SUCCESS,
  credential:credential.data
});

export const loginWithEmailFailure = error => ({
  type: types.LOGIN_WITH_EMAIL_FAILURE,
  error
});

redux的历史如下:

history of redux

0 个答案:

没有答案