我的 api 在任何地方都运行良好,除了当我尝试执行 if 块以解决潜在的 Axios 错误时。这是因为status
在 中使用了 isresponse.response.status
而不是response.status
typescript 会抛出错误Property 'response' does not exist on type 'AxiosResponse<any, any>'.
有人能帮我吗?
在没有 AxiosError 的情况下工作:
postBoundary: async (boundary: CreateBoundary) => {
const response = await axios.post(
`${config.baseApiPath}boundary`,
boundary
);
return response.data; //returns correctly
}
无法使用 Axios 出现错误:
postBoundary: async (boundary: CreateBoundary) => {
const response = await axios.post(
`${config.baseApiPath}boundary`,
boundary
);
// when response has an Axios error response.status
// doesn't exist because there are two nested responses
if (response.response.status ===
HttpStatusCode.PayloadTooLarge) {
throw new Error("Error");
}
return response.data;
}
这是因为当 Axios 请求失败时,错误对象的结构与成功响应不同。