【问题标题】:Express API DELETE快速 API 删除
【发布时间】:2017-12-18 16:34:20
【问题描述】:

我正在尝试将 DELETE 添加到我的 api,但我尝试的所有操作都收到 404: Not Found。所有 GET 和 POST 方法都有效。这是使用包含点的“点”模型,每个点都有一个唯一的 id。我正在尝试添加一个调用以通过其 id 删除一个点。

动作

export function deletePointById(identifier) {
    return dispatch => {
        return axios.delete('/api/points/' + identifier)
    }
}

删除路线(不起作用)

router.delete('/:identifier', (req, res) => {
    Points.remove({
      id: req.params.identifier
    }), function (err, user) {
      if (err) {
        return res.send(err);
      }

      res.json({ message: 'Deleted' });
    };
});

这是一个可以正常工作的现有 GET

动作

export function getPointsBySession(session){
    return dispatch => {
        return axios.get('/api/points/session/' + session)
    }
}

GET 路径

router.get('/session/:session', (req, res) => {
    Points.query({
      select: ['id', 'number', 'quadrant', 'level', 'title', 'category'],
      where: {sessionId: req.params.session}
    }).fetchAll().then(point => {
      res.json({ point });
    })
});

【问题讨论】:

    标签: reactjs express axios


    【解决方案1】:

    您的问题可能是代码中多了一个大括号和分号 (};):

    router.delete('/:identifier', (req, res) => {
        Points.remove({
          id: req.params.identifier
        }), function (err, user) {
          if (err) {
            return res.send(err);
          }
    
          res.json({ message: 'Deleted' });
        }; // <-- HERE
    });
    

    您还需要删除我添加评论的行上方的分号。

    看起来像这样:

    router.delete('/:identifier', (req, res) => {
        Points.remove({
          id: req.params.identifier
        }), function (err, user) {
          if (err) {
            return res.send(err);
          }
    
          res.json({ message: 'Deleted' })
    });
    

    【讨论】:

      猜你喜欢
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多