Express API DELETE

问题描述 投票:0回答:1

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

行动

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

DELETE的路由(不起作用)

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个回答
0
投票

看起来您的问题可能是代码中有一个额外的括号和分号(qazxsw poi):

};

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

它看起来像这样:

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
});
© www.soinside.com 2019 - 2024. All rights reserved.