路由以从材料表中删除数据?"/>
我如何设置我的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路由以从材料表中删除数据?
发布评论