xhr.js:210 DELETE http://localhost:3000/posts/62575cb61cb27c6417732193 403 (Forbidden) / cannot delete document

I am using axios for the request of my own api, everything works correctly except the DELETE request, I looked for information about the error but I have not found the solution. when I send the request to the server I get this error: "xhr.js:210 DELETE 403 (Forbidden)".

I put this line of code in Package.Json to avoid problems with CORS:

"proxy": "" 

This would be my api, for the operation I pass the post id by url and the user id: (I tried it in postman and it works without any problem)

router.delete("/:id", async (req, res) => { try { const post = await Post.findById(req.params.id); if (post.userId === req.body.userId) { await post.deleteOne(); res.status(200).json("the post has been deleted"); } else { res.status(403).json("you can delete only your post"); } } catch (err) { res.status(500).json(err); } }); 

and this is where I consume my api:

const Post = ({ date, _id,userId, description }) => { const handleDelete = async () => { try { await axios.delete('posts/' + _id, { userId: currentUser._id }) } catch (error) { console.log(error) } } return( <div onClick={handleDelete }> //component content </div> ) } export default Post 
2

1 Answer

I solved it, sending a data object to my api (inside that object I put the user id):

const handleDelete = async () => { try { await axios.delete('/posts/' + _id, { data: { userId: currentUser._id } } ) } catch (error) { console.log(error) } } 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like