我如何设置我的API路由以从材料表中删除数据?

编程入门 行业动态 更新时间:2024-09-27 09:21:32

我如何设置我的API<a href=https://www.elefans.com/category/jswz/34/1771390.html style=路由以从材料表中删除数据?"/>

我如何设置我的API路由以从材料表中删除数据?

我正在研究一个完整的项目。对于前端,我正在使用React。对于后端,我使用Node,Express,Sequelize和Postgres。在我的页面上,我具有Material-table设置,它正在从后端获取数据。同时,我希望可编辑功能可用于在表中添加,更新和删除。

API ROUTE

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

用于删除的材料表可编辑的设置

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
          });
    };
};

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

以某种方式,我认为问题出在我的路线以及传递给材料表的网址上。当我删除材料表中axios请求的url中的:id时,即http://localhost:8004/api/v1/locations/delete,我收到错误消息,即控制台中的id未定义。

回答如下:

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

您将需要自行调用该特定网址:

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

更多推荐

我如何设置我的API路由以从材料表中删除数据?

本文发布于:2024-05-07 11:26:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   如何设置   材料   数据   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!