Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  48] [ 2]  / answers: 1 / hits: 6212  / 3 Years ago, tue, march 2, 2021, 12:00:00

I'm tried to fatch my api data from backend by using axios. I'm getting an error, like this:


POST http://localhost:3000/undefined/post 404 (Not Found)


API routes like this:


// route middleware
app.use("/api", portRoutes);

// passing on controllers
router.post("/post", create);

// rest of code will have in controller folder

Now I have tried to work in frontend


I have tried by this way:


.env file


REACT_APP_API = http://localhost:8000/api


I dont know why does not access my server side links


handleSubmit function


const handleSubmit = (e) => {
e.preventDefault();

// access data from backend
axios
.post(`${process.env.REACT_APP_API}/post`, { title, content, user })
.then((response) => {
console.log(response);
setState({ ...state, title: "", content: "", user: "" });
alert(`Post Title ${response.data.title} is created`);
})
.catch((error) => {
console.log(error.response);
alert(error.response.data.error);
});
};

I'm sure my api is ok, I have checked my api with postman software.


More From » node.js

 Answers
9

I have solved my error from this link
Please feel free read this article if you faced same problem, that I have faced.


Thank you.


[#1708] Thursday, February 25, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josuea

Total Points: 609
Total Questions: 121
Total Answers: 104

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
josuea questions
Fri, Apr 17, 20, 00:00, 4 Years ago
Fri, Sep 27, 19, 00:00, 5 Years ago
Sat, Jun 8, 19, 00:00, 5 Years ago
;