// START EIDT:
Solution was (thanks to mr. Hawkeye @robertklep) the name "body" in my post request in axios on the client side.
// END EIDT:
I'm currently building an application with React as the frontend and Express as backend.
For API calls I'm using axios. My APIhas been working fine, but all of the sudden with no obvious change my post request body is not being sent to the backend.
In my index.js I had the following:
app.use(bodyParser.json());
but also tried with
app.use(express.json())
Client side function calling my post request:
const checkForExistingUserData = async () => {
if (user) {
const body = {
caseId: user['caseId']
}
await postRequest({request: body}, commonApiLocations["userDataLookUp"])
}
}
My client side call is being call this way:
export function postRequest(body, url) {
console.log(body) // Logs correct body with my data inside
const config = {
method: 'post',
url: masterEndPoints['expressEndPoint'] + url,
headers: {
'Content-Type': 'application/json',
},
body // should be called "data" (see edit in top)
};
return axios(config);
}
Express route endpoint function:
route.post('/api/users/checkExistingUserData', async (req, res) => {
try {
const request = req.body.request;
console.log("request: ", request); // Gives undefined
if (request['caseId']) {
let result = await checkIfExist(tableNames['TableName'], {'caseId': request.caseId})
res.status(200).send(result);
} else {
res.status(200).send(false)
}
} catch (error) {
res.status(500).send('En error occurred on the server')
}
});
When I console.log on on the client side I see my request with the data that I expect to see.
When I console.log on the server I get an undefined.
When I console.log req.body I get "{}"
Any reasons for this error? I'm completely dumbfounded by this.