Home > Enterprise >  Unable to send data from react to express server
Unable to send data from react to express server

Time:10-26

I am trying to send data from react to express server.

React code:

const imageRenderer = async () => {
  const res = await axios({
    method: "post",
    url: "http://localhost:8080/previewRender",
    data: selectedFile.filePaths,
  });
console.log(res.data);

};

Express code:

app.post("/previewRender", (req, res) => {
  var fileLocation = req.body;
  console.log(fileLocation); 
});

However in console the filelocation is shown as undefined. selectedFile.filePaths is of type object. Can't I send object to express?

CodePudding user response:

Add this in app.js (app.js) or your root file of the app to post req

app.use(express.json());

app.use(express.urlencoded({ extended: false }));

Put this middleware in app.js above your routes...

CodePudding user response:

Did you use app.use(express.json()) in your app.js?

  • Related