I have a node.js back-end server. Front-end is running on Angular 10.
I want to pass data from front-end to back-end using fetch
Front-end code :
testmariadb($event: MouseEvent) {
return fetch('/api/customQuery', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
sqlQuery: 'SELECT * FROM software'
})
})
}
Back-end code
app.post('/api/customQuery', async (req, res) => {
let test = req.body['sqlQuery']
console.log(test)
})
I get this error :
let test = req.body['sqlQuery']
^
TypeError: Cannot read properties of undefined (reading 'sqlQuery')
at /home/erwan/Documents/GitHub/serverAltertManager/index.js:90:22
What I'm doing wrong ?
Copyright Notice:Content Author:「Lorteau Erwan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72854442/how-do-i-send-json-data-to-node-js-server-using-fetch