首页 > 解决方案 > 如何修复错误“无法读取未定义的属性‘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,所以我不知道如何解决它。

标签: javascriptreduxaxios

解决方案


在您的操作文件中删除

将第 3 行替换为

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


推荐阅读