javascript - 未处理的拒绝(TypeError):无法读取未定义的属性“statusText”
问题描述
我在尝试使用 react-redux 实现删除功能时遇到了这个问题。
动作/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 }
});
}
};
减速器/profile.js
case UPDATE_PROFILE:
return {
...state,
profile: payload,
loading: false
};
case PROFILE_ERROR:
return {
...state,
error: payload,
loading: false
};
解决方案
问题出在删除 API 中。API应该像这样工作http://localhost:5000/api/profile/experience/5df8f54012b7a81ac04d6b25但不知何故它也以这种方式工作http://localhost:5000/api/profile/experience/5d与用户的令牌。所以我改变了api代码
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')
}
至
try {
const foundProfile = await Profile.findOne({ user: req.user.id });
const eduIds = foundProfile.education.map(edu => edu._id.toString());
const removeIndex = eduIds.indexOf(req.params.edu_id);
if (removeIndex === -1) {
return res.status(500).json({ msg: "Server error" });
} else {
foundProfile.education.splice(removeIndex,1);
await foundProfile.save();
res.json({ msg: 'Education Deleted'})
}
} catch (error) {
console.error(error);
return res.status(500).json({ msg: "Server error" });
}
现在它工作正常。
推荐阅读
- java - 为什么 Eclipse 建议使用 ArrayList
- php - 带数组的php mysqli查询
- regex - 在 Grep for Windows 命令行中使用正则表达式
- javascript - 如何在解决之前等待 nedb 查询完成?查询有时会超出承诺
- html - 角材料扩展表
- mongodb - 如何在 MongoDB 中查找包含空值的文档的数量?
- python - 如何在 Python 中搜索字符串中的列表项?
- python - Python csv reader for row in reader - 一次性下划线在这段代码中代表什么?
- linux-kernel - u-boot 代码中的自定义 DDR 控制器初始化
- android - 在 TextField 中输入的文本未添加到列表中