Redux-Saga响应未定义

时间:2020-06-05 14:18:30

标签: axios redux-saga

我使用redux-sagas和axios对请求的响应未定义

export function* getCompanies({ payload }: any) {
  try {
    const { data: response } = yield call(api.get, '/companies-list', {
      params: {
        keyword: payload,
      },
    });
    console.error(response); // RETURNS NULL
    const { data } = response;
    yield put(handleFetchCompaniesAsync.success(data.companies));
  } catch ({ response }) {
    yield all([
      put(handleFetchCompaniesAsync.failure(response.data)),
      put(handleAsyncNotification(response.data)),
    ]);
  }
}

尝试记录响应时,它会显示null

1 个答案:

答案 0 :(得分:0)

您的传奇看起来还不错,应该可以正常工作

我认为您应该检查api.get端点。如果您不返回自己的fetch通话,则可能是此undefined的原因。

例如:

--V-- this is probably what your are missing
return fetch('http://localhost:3000')
        .then((res) => {
            // bla bla bla
            return res.json();
        })
        .catch((err) => {
            // bla bla bla
        });