0

Technologies: [ {Frontend: 'Vue-js 2'}, {Backend: 'Node-js / Express / SQL'} ] I need to send a comment comming from a form-data with 3 entries (content / userID / PostId)

Front-side I have all the values setup. Then in my backend when I log req.body I got an empty Object...

Why my form data is empty ? or is my backend not able to read it ?

Function from my front :

 newComment(idPost) {
  const comment = new FormData();
  comment.append('content', this.comment);
  comment.append('userId', this.user.userId);
  comment.append('postId', idPost);

  for (let pairs of comment.entries()) {
    console.log(pairs);
  }
  const headers = {
    'Content-type': 'application/json',
    Authorization: 'Bearer ' + this.user.token,
  };
  axios
    .post(`${url}comment`, comment, { headers }).then(etc....)

In my logs : form data entries log

Function from my back :

  exports.createComment = (req, res, next) => {
  console.log('new comment............')
  console.log(req.body);
  const content = req.body.content;
  const userId = req.body.userId;
  const postId = req.body.postId;
  db.query(
    `INSERT INTO comments (content, userId, postId) VALUES ('${content}', '${userId}', '${postId}')`,
    (err, result, fields) => {...}

In my logs : backend req.body logs

  • https://stackoverflow.com/questions/9304888/how-to-get-data-passed-from-a-form-in-express-node-js – boxdox Mar 30 '22 at 13:04
  • 1
    You seem to be facing the same issue as this : https://stackoverflow.com/questions/56688512/formdata-server-side-nodejs, you can go check if that solves your issue. – liguepk Mar 30 '22 at 16:11

0 Answers0