【发布时间】:2022-01-12 19:05:56
【问题描述】:
我正在开发一个后端 API,调用该 API 的 React 前端随机获得一个 Network Error 或 Cannot read properties of undefined (reading 'data')。我一直无法重现这种行为。它正在前端的日志中报告,并且它没有显示在后端的日志中,据我所知,请求甚至没有命中 API。前端使用 Axios 进行 API 调用。下面是出现这些错误的两个调用的示例。此代码中的任何内容都可能导致这些错误?
const params = `email=${this.queryParams.email}`;
const response = await axiosStore.axiosApi.get(`/data?${params}`).catch(error => {
console.error(error);
});
const values = await response.data;
this.setValues(values);
const response = await axiosStore.axiosApi.post(`/data`, { data: mappedChanges }).catch(error => {
if (error && error.response && error.response.data && error.response.data.errors) {
const { errors } = error.response.data;
const message = Object.keys(errors)
.map(x => errors[x])
.join(', ');
throw new Error(message);
} else {
throw new Error('Something went wrong');
}
});
newObject = response.data;
【问题讨论】:
标签: reactjs api axios frontend