Below is my frontend code i have to submit a post request from react.
const { name, email, message } = greeting;
const payload = {
name: name,
email: email,
message: message,
};
console.log(payload);
fetch("http://localhost:8080/greetings", {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(payload),
})
.then(async function (res) {
let response = await res.json();
console.log(response);
})
.catch((err) => console.log(err));
Below is my express code for post
`
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/greetings", async (req, res) => {
const { name, email, message } = req.body;
console.log(req.body);
db();
const saveDocument = new Document({
name: name,
email: email,
message: message,
});
await saveDocument
.save()
.then((savedDoc) => {
res.send({ status: 200, mes: "success", request: req.body });
})
.catch((e) => console.log(e));
});
`
I have tried to submit a post using postman and i was able to see data in my mongoDB. But from my frontend i always get an empty object in the request.body.