javascript - 如何修复错误“无法读取未定义的属性‘statusText’”
问题描述
因此,当我尝试在 webapp 中删除教育/经验字段时出现此错误。但是当我按下删除时,它会抛出一个错误:
Unhandled Rejection (TypeError): Cannot read property 'statusText' of undefined
当它尝试发送有效负载时会发生这种情况。这是导致错误的代码:
161 | } catch (err) {
162 | dispatch({
163 | type: PROFILE_ERROR,
> 164 | payload: { msg: err.response.statusText, status: err.response.status },
165 | ^ });
166 | console.log(err.response);
167 | }
这是删除的操作文件:
export const deleteEducation = (id) => async (dispatch) => {
try {
const res = axios.await(`/api/profile/education/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});
dispatch(setAlert('Education Deleted', 'success'));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status },
});
console.log(err.response);
}
};
以下是删除功能的 API 代码:
// @route DELETE api/profile/:edu_id
// @desc Delete education from profile
//@access Private
router.delete('/education/:edu_id', auth, async (req, res) => {
try {
const profile = await Profile.findOne({ user: req.user.id });
const removeIndex = profile.education
.map((item) => item.id)
.indexOf(req.params.edu_id);
profile.education.splice(removeIndex, 1);
await profile.save();
res.json({ msg: 'Education Deleted' });
} catch (err) {
console.log(err.message);
res.status(500).send('Server Error');
}
});
我很难理解 redux,所以我不知道如何解决它。
解决方案
在您的操作文件中删除
将第 3 行替换为
const res = await axios.delete(`/api/profile/education/${id}`);
推荐阅读
- zsh - brew 升级后 PoweLevel9k 崩溃
- python - 在 python try-except 子句的 unittest 中未捕获 AssertError
- python-3.x - 我在我的代码中两次使用相同的模型,如何避免转移权重?
- java - 在不检查存在的情况下解决损坏的符号链接
- html - Safari中的引导列+垂直选项卡受到干扰
- android - 如何计算Android手机从手上掉下来的垂直距离?
- reactjs - 如何在 React.js 中获取 URL 值?
- katalon-studio - 我们可以为katalon studio中的文本字段生成随机的唯一字母吗?
- android - 获取麦克风 ID Android API < 23
- c# - C# 使用丢弃'_'