如何修复错误“无法读取未定义的属性‘statusText’”

时间:2021-05-29 10:40:41

标签: javascript redux axios

因此,当我尝试删除 web 应用程序中的教育/经验字段时出现此错误。但是当我按下删除时,它抛出了一个错误:

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,所以我不知道如何修复它。

1 个答案:

答案 0 :(得分:2)

在您的操作中删除文件

替换第三行

const res = await axios.delete(`/api/profile/education/${id}`);