React - 删除请求带有错误代码 404

发布于 2025-01-11 13:28:11 字数 1018 浏览 0 评论 0原文

我在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖不如嘢 2025-01-18 13:28:11

axios 应小写:

axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)

请注意表达代码上的右括号:

app.delete('/vehicleDelete/:CarId', async (req, res) => {
    db.query("DELETE FROM products WHERE id = ?", req.params.CarId, (err, result) => {
        if (err) return res.status(500).send('Error')
        res.status(200).send(result);
    })
})

axios should be lowercased:

axios.delete(`http://localhost:3001/vehicleDelete/${CarId}`)

Be careful with the closing parentheses on the express code:

app.delete('/vehicleDelete/:CarId', async (req, res) => {
    db.query("DELETE FROM products WHERE id = ?", req.params.CarId, (err, result) => {
        if (err) return res.status(500).send('Error')
        res.status(200).send(result);
    })
})
孤芳又自赏 2025-01-18 13:28:11

您应该运行此命令:

app.delete('/vehicleDelete/:CarId'), (req, res) => {
   // make sure your are getting CarId that exists
   // and then you delete it

    db.query(`DELETE FROM products WHERE id = ${req.params.CarId}`,
        (err, result) => {
            console.log(err)
            console.log(result)
            if (result) {
                res.send(result);
            }
        })
}

此外,您不需要添加 async 因为您不使用 await 进行查询。结果会为您提供一个可能如下所示的对象:

{
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 34,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

现在,当您说您收到 404 状态代码时,这意味着您没有发出请求的路由。因此,http://localhost:3001/vehicleDelete/${CarId} 您需要在服务器上正确注册路由。

您应该使用 Promises 添加 catch 块,建议这样做。

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")
                }
            }).catch(console.log);
    }

You should run this:

app.delete('/vehicleDelete/:CarId'), (req, res) => {
   // make sure your are getting CarId that exists
   // and then you delete it

    db.query(`DELETE FROM products WHERE id = ${req.params.CarId}`,
        (err, result) => {
            console.log(err)
            console.log(result)
            if (result) {
                res.send(result);
            }
        })
}

Also, you don't need to add async as your not using await for the query. The result gives you an object that might look like this:

{
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 34,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

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.

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")
                }
            }).catch(console.log);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文