【发布时间】:2020-04-09 19:58:01
【问题描述】:
我在尝试使用 react-redux 实现删除功能时遇到了这个问题。
action/profile.js
export const deleteExperience = id => async dispatch => {
try {
const res = await axios.delete(`/api/profile/experience/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data
});
dispatch(setAlert("Experience Removed", "success"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
}
};
reducers/profile.js
case UPDATE_PROFILE:
return {
...state,
profile: payload,
loading: false
};
case PROFILE_ERROR:
return {
...state,
error: payload,
loading: false
};
【问题讨论】:
-
但是如果
Cannot read property 'statusText' of undefined在catch 内,你会在哪一行得到Cannot read property 'statusText' of undefined- 首先尝试console.logerr.response并查看存在哪些属性 -
@Zydnar 我试图 console.log err.response 但我什么也没得到。实际上,删除工作正常,正在从数据库中删除条目。但不是显示成功警告 catch 块代码正在运行
-
如果删除有效,这意味着运行了 try 块,但这并不意味着错误是 somwere else
Cannot read property 'statusText' of undefined这意味着err.response未定义 - 也许您遇到了错误类型的错误。将 catch 块的内容包装在if(err.response){中,否则为 console.error(err) -
您可以尝试暂时删除此行
dispatch(setAlert('Experience Removed', 'success'))吗?如果可行,您可以在问题中添加 setAlert 代码吗?