admin管理员组文章数量:815140
我如何设置我的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路由以从材料表中删除数据
版权声明:本文标题:我如何设置我的API路由以从材料表中删除数据? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1715173032a821932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论