如何设置 API 路由以从 Material table 中删除数据?

How do I setup my API route to delete data from Material table?

我正在做一个全栈项目。对于前端,我正在使用 React。对于后端,我使用的是 Node、Express、Sequelize 和 Postgres。在我的页面上,我有 Material-table 设置,它正在从我的后端获取数据。同时,我希望 editable 功能用于从 table.

添加、更新和删除

API 路线

router.delete('http://localhost:8004/api/v1/locations/delete/:id', locationController.deleteLocation);

MATERIAL-TABLE EDITABLE 删除设置

onRowDelete: oldData =>
                new Promise(resolve => {
                  setTimeout(() => {
                    resolve();
                    const data = [...results.data];
                    data.splice(data.indexOf(oldData), 1);
                    axios
                    .delete("http://localhost:8004/api/v1/locations/delete/:id", {
                    params: {
                        id: results.data[0].id
                    }
                })
                .then(res => console.log(res.data));
                    setResults({ ...results, data });
                  }, 600);
                }),

用于删除路由的快速控制器

exports.deleteLocation = async (req, res) => {
    try {
    await Location.destroy({ where: { id: req.params.id}}); 
      res.status(200);
      res.json(
        {
          message: 'One Location deleted'
        }
      );
  }
    catch(err) {
      console.log(err);
      res.status(500).json({
            message: "There is an error deleting the Location!", err
          });
    };
};

当我点击 material-table 中的删除图标时,该项目将被删除,但在重新加载时它会出现。当我检查我的控制台时,我收到查询 DELETE FROM "location" WHERE "id" = ':id'

不知何故,我认为问题出在我的路线和我传递给 material-table 的 url 上。当我在 material-table 中删除 axios 请求的 url 中的 :id 时,即 http://localhost:8004/api/v1/locations/delete 我收到控制台中未定义 id 的错误。

axios 中的 delete 请求不会自动将 url 中的 :id 转换为参数中的 id

您需要自己调用特定的 url:

axios
.delete(`http://localhost:8004/api/v1/locations/delete/${results.data[0].id}`)