I have a React app where I’m trying to create a POST request using Axios, but it doesn’t seem to work. The strange part is I have checked my backend route and tested it with Nodemon. everything is working fine there but I don’t understand why the route is not working in the front end
Frontend – This is where I tested my endpoint in nodemone and it worked just fine.
API is working fine too.
and this is my code in the front end
The error I’m getting:
2
Answers
I have read your code and I found some issues. You can improve it by some steps:
1- Defined an async function fetchComments inside useEffect and called it immediately.
2- Used currentUser passed as a prop to set the username in commentToSend.
3- When a new comment is posted, it prepends the new comment to the existing comments.
It would be better to use ES6, for example arrow function.
Here’s the correct code
If I were you, I would have used Redux-toolkit also to presist data or some other tools like react-query to cache request for better performance.
Try to use the console log to see the EndPoint URL
When testing the same POST request on my side (username: "butter_bridge"), the response I got was 404: "No user found for article_id"
So, my bet is that the issue rather lies with the API – the given username and the article are not compatible somehow 🙂