0

I'm trying to create a contact form in my react app. Every time I submit the form my Express server only receives the parameters that are empty.

Here is my server side code:

app.use(express.json());

router.post("/contact", (req, res) => {
  console.log(req.body); // prints {name: "", subject: ""}
  res.status(200).send("Success");
});

My axios request:

axios.post(
  `http://localhost:9000/.netlify/functions/api/contact`,
  {
    name: name,
    emailAddress: email,
    subject: subject,
    message: message,
  },
  {
    headers: {
      "Content-Type": "application/json",
    },
  }
);

It only prints prints {name: "", subject: ""} but I am filling out email address and message fields as well.

I have tried using bodyParser but it gives me deprecation notice as I am using Express 4.17.1.

What am I doing wrong?

Tehreem
  • 939
  • 2
  • 14
  • 31
  • 1
    Axios drops undefined fields so you cannot see them on the server-side (undefined values do not convert to JSON), would you please post your network's payload too! – Raeisi Jun 13 '21 at 15:58
  • That's exactly what my issue was. The state wasn't getting updated and the variable stayed undefined. Resolved now. Thanks – Tehreem Jun 13 '21 at 16:26

0 Answers0