繁体   English   中英

axios 错误反应拦截器请求不是函数

[英]axios error react interceptors request is not a function

我有一个带有 axios 的配置我正在测试一个从 api 接收学生列表的功能,问题是它向我发送了一个错误:

TypeError: constants_api_constants__WEBPACK_IMPORTED_MODULE_1 _.default.interceptors.request 不是函数

对于我的 axios 配置,我使用:

const options.GET_ALL_STUDENTS = {
  method: "GET",
  url: "/Student",
}
const BASE_API_URL = "https://localhost:7072/api";
const api = axios.create({
  baseURL: `${BASE_API_URL}`,
});

const getStudents = () => {
  return api.interceptors.request(options.GET_ALL_STUDENTS).use(
    function request(success) {
      return success;
    },

    function error(err) {
      return err;
    },
  );
};

我如何解决我的承诺,(没有拦截器可以正常工作):

function* fetchStudents() {
  try {
    const result1 = yield call(getStudents);
    const studentList = createStudentListAdapter(result1.data);
    yield put(fetchStudentsSuccess(studentList));
  } catch (error) {
    yield put(fetchStudentsFailure());
  }
}

拦截器用于在任何request/response进入try/catch之前拦截它。

const getStudents = async () => {
  try {
    const res = await api(options.GET_ALL_STUDENTS);
    // logic
  } catch (e) {
    // handle error
  }
};

拦截器

api.interceptors.request.use(
  (request) => {
    console.debug("Request", request.url);
    return request;
  },
  (error) => {
    console.debug("Request Failed", error.request.data.message);
    return Promise.reject(error);
  },
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM