Skip to content
Advertisement

formData in React, getting null when I send formdata to backend Express

When I send data from the frontend I receive null in the backend. I am sending 2 string data URLs and dates so I don’t think that I need to use extra middleware for receiving the values.

Frontend:

JavaScript

Backend:

JavaScript

Advertisement

Answer

it’s empty because on the server you have a json parser, but you’re sending a multipart/form-data request, and there is no parser to process it.

as you’re not uploading a file, the easiest way would be to send a json request:

JavaScript

if you want to use FormData, you need to include parser on the server. usually, this is multer, but as you’re not uploading a file, use body-parser

JavaScript

however, as FormData is sending data as multipart/form-data, bodyParser cannot parse it, so you need to turn it into URL-encoded string with URLSearchParams

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement