'I can't call PATCH method using Pug, Mongoose and Express.js
I am new to code and I apologize in advance for all my noob mistakes.
I'm trying to set a new color to an existing object stored in MongoDB using the PATCH method in the browser, but I don't understand why its not working while the GET and POST methods are working.
I have to mention that the PATCH method works in Postman.
The code in my pug file:
form(action=`/posts/changeColor` + id, method="patch")
label Enter variable color:
input(type="text", name="color")
button(type="Submit") Submit
And the code in express:
router.patch('/posts/changeColor/:id', async(req, res)=>{
try{
const test = await products.updateOne({_id: req.params.id},{$set:{productColor: req.body.color}});
res.json(test);
}catch(err){
console.log(err);
}
}
This is the error I get after pressing the "Submit" button
Cannot GET posts/changeColor/62723c3ed31baa41d5e9b0e1
Solution 1:[1]
HTML forms can not initiate PATCH
HTTP requests.
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-method
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jonas Eberle |