React - 删除请求带有错误代码 404
我在尝试从 mySQL 数据库中删除驱动程序时遇到问题。 调用我的函数并传递映射的 id(它正在工作):
<button id="deleteRent" onClick={DeleteVehicles.bind(vehicle.id)}>Delete</button>
这是我的反应代码:
const DeleteVehicles = (CarId) => {
Axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)
.then((response) => {
if (response) {
console.log(response)
alert("Sikeres Törlés")
navigate("/admin");
}
else {
console.log("törlési hiba")
}
})
}
这是我的节点表达请求:
app.delete('/vehicleDelete/:CarId'), async (req, res) => {
db.query("DELETE FROM products WHERE id = ?", req.params.CarId,
(err, result) => {
console.log(err)
console.log(result)
if (result) {
res.send(result);
}
})
}
有什么想法吗?
I've an issue while i'm trying to delete a driver from mySQL db.
Calling my function and passing mapped id (it's working):
<button id="deleteRent" onClick={DeleteVehicles.bind(vehicle.id)}>Delete</button>
Here is my react code:
const DeleteVehicles = (CarId) => {
Axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)
.then((response) => {
if (response) {
console.log(response)
alert("Sikeres Törlés")
navigate("/admin");
}
else {
console.log("törlési hiba")
}
})
}
and here is my node express request:
app.delete('/vehicleDelete/:CarId'), async (req, res) => {
db.query("DELETE FROM products WHERE id = ?", req.params.CarId,
(err, result) => {
console.log(err)
console.log(result)
if (result) {
res.send(result);
}
})
}
any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
axios
应小写:请注意表达代码上的右括号:
axios
should be lowercased:Be careful with the closing parentheses on the express code:
您应该运行此命令:
此外,您不需要添加
async
因为您不使用await
进行查询。结果会为您提供一个可能如下所示的对象:现在,当您说您收到
404
状态代码时,这意味着您没有发出请求的路由。因此,http://localhost:3001/vehicleDelete/${CarId}
您需要在服务器上正确注册路由。您应该使用
Promises
添加 catch 块,建议这样做。You should run this:
Also, you don't need to add
async
as your not usingawait
for the query. The result gives you an object that might look like this:Now, when you say you receive the
404
status code, it means that you don't have the route on which the request is made. So,http://localhost:3001/vehicleDelete/${CarId}
you need to register the route properly at the server.You should add the catch blocks with
Promises
, it is recommended practice.