Enrique Ajin
Enrique Ajin

Reputation: 5

POST method using Axios only inserts ID in the database. React JS

I'm trying to make a POST request using Axios in React. Apparently isn't problem because the status returned is 200 "OK" but only the ID is inserted in the database. I'm using the following snippet to do the request:

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

axios.defaults.baseURL = "http://localhost:8000";

try {
  console.log(this.state.form);
  const response = await axios.post("/create", this.state.form);
  console.log(response);
} catch (error) {
  console.error(error);
}

};

I test it in Postman, and it seems there is no problem. Test in Postman

But when I'm trying to create a new register from the front end, only inserts the ID in the database. Maybe this picture could be useful. Response in console

Upvotes: 1

Views: 520

Answers (1)

Sahar Levy
Sahar Levy

Reputation: 89

The axios.post("/create", this.state.form) that you made isn't equivalent to the Postman request

In the Postman request you add the params as url params And in the Axios request you send as body params

So What you need to do is to send the Axios request like this:

await axios.post(`/create?firstname=${this.state.form.firstname}&lastname=${this.state.form.lastname}&salary=${this.state.form.salary}`,{});

And thats should work for you, if not - add the server router code

Upvotes: 2

Related Questions