Home > Blockchain >  delete method is returning 404 error while creating a rest api
delete method is returning 404 error while creating a rest api

Time:06-27

I was creating a rest api and the delete request is not being read. this was the code

app.delete("/students:/id", async (req, res) => {
    console.log("in delete")
    try {
        const _id = req.params.id
        const delete_student = await Student.findOneAndDelete(_id)
        console.log(req.params.id)
        if(!req.params.id){
            return res.status(400).send()
        }
        res.send(delete_student).status(200)
    } catch (error) {
        res.status(500).send(error)
    }
})

and this is the response

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot DELETE /62b93a487624a06ef8756439</pre>
</body>

</html>

CodePudding user response:

The path should be declared as:

app.delete("/students/:id", async (req, res) => { ... })
  • Related