DEV Community

Rais
Rais

Posted on

formdata posted in reactjs to nodjes backend arriving empty

so I have a nodejs backend which am trying to make a post request to using axios and am collecting the data at the frontend with formdata , all checks indicates that the formdata is populated however when it gets to the backend the formdata is empty, I tried working on my headers however nothing worked so far… I am could use some expert opinion

//FrontEnd Code
const handleSubmit = async (e)=>{

  e.preventDefault();
 // console.log(bodyFormData)
 bodyFormData.append("email", email);
  bodyFormData.append("first_name", fName);
  bodyFormData.append("last_name", lName);
  bodyFormData.append("password", password);
  //Axios config
  const request = {
    method: "post",
    url: "http://localhost:9000/client/sign-up",
    body: bodyFormData,
    headers: {
      'Access-Control-Allow-Origin':'*',
      " content-Type":"application/json", 
      "X-Request-Platform": platform,
     },
  };  
  try {
    console.log(bodyFormData, request.body)
    console.log(platform)
    //const hashpassword = await bcrypt.hash(e, 10)
    const res= await axios( request)
     console.log(res.data)

//Form Data shown attached  from console log in browser's console, however //delivers an empty body at the backend
FormData(4) { email → "gunter@ilmenau.com", first_name → "dfdadf", last_name → "sdfsdsdf", password → "test1212" }

Enter fullscreen mode Exit fullscreen mode

App.js backend
const cors = require('cors');

const bodyParser = require('body-parser')

const multer = require('multer');

const upload = multer();

const forms = multer();

mongodb_connection().then(() => {

console.log("App is connected to database");

});

(async () => {

await worker.start();

})();

const express = require("express");

const app = express();

app.use(cors());

app.use(express.json());

// for parsing application/json

app.use(bodyParser.json());

// for parsing application/xwww-

app.use(bodyParser.urlencoded({ extended: true }));

//form-urlencoded

// for parsing multipart/form-data

app.use(upload.array());

app.use(forms.array());

app.use(express.static('public'));

// parse application/x-www-form-urlencoded

However the formData always arrive empty

App is connected to database
Worker is connected to database
{}
Error creating user: Error: users validation failed: password: Cast to string failed for value "Promise { }" (type Promise) at path "password", email: Path email is required., first_name: Path first_name is required., last_na

PS: I have tweaked my headers and added and subtracted bodyparses a couple of times now am out of ideas

Top comments (0)