How do I send JSON data to node.js server using fetch?
NickName:Lorteau Erwan Ask DateTime:2022-07-04T17:21:33

How do I send JSON data to node.js server using fetch?

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

Answers
DEV 2022-07-04T09:25:44

Are using the middleware properly\nconst express = require('express');\nconst app = express();\n\napp.use(express.json());\n",


More about “How do I send JSON data to node.js server using fetch?” related questions

How do I send JSON data to node.js server using fetch?

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...

Show Detail

How to send XML data through body to node js server using fetch method

I am trying to send the XML data to node server using fetch method but I am not able get that data in node API. I am able to give API call through fetch. Below is code for fetch method. The (result...

Show Detail

How to send data using fetch and POST request with credentials included?

I want to send some data in JSON from my React front-end on port 3000 using fetch, to my node.js server on 3005. I configured cors on my server, but every time I try to send request with cookies, C...

Show Detail

send post from webpage to Node.js server ...do not use 'fetch'...?

I am using the following javascript on a webpage to send information to a Node.js server upon a "click" on an image. This is using a 'POST' request. <script> function rerouter(_sent) { var

Show Detail

Ajax send JSON and fetch data using PHP

I have jquery code output json data and I need to send this data to to url and fetch using php Code: JSON.stringify(showtimes) Output: [{"show_id":"1","movie_id":"20","cinema

Show Detail

Send data on a JSON server using react

am trying to send a post request to a JSON server using react. I've captured the data from a form using react formik. When I try to make a post request, the server only creates an id but the rest o...

Show Detail

Fetch problem with node.js express server

I'm having some trouble with the fetch and node.js. In my frontend when i click a button, i would like to send a post request in order to receive an array from my backend as answer. I'n my backend ...

Show Detail

Send data between GraphQL Node.js server and React in Nx

I setup two projects, Node.js and React in Nx monorepo. I would like to use GraphQL for communication. Projects I'm running with command nx serve api(Node.js) and nx serve totodile (React). Problem...

Show Detail

Fetch data in Json array and send it to server

I am developing an android application where I am fetch all the data in j son array. Now i want to send said Json array to MySQL database on server. db.open(); ArrayList<HashMap<String,

Show Detail

Send json data to Node.js with Socket.io

I am doing a project in Node.js using socket.io technology. Here's the code I wrote in html: <script type="text/javascript" src="data.json"></script> <script type="text/javascri

Show Detail