簡體   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