0

I have a code like this (pic 1). I want to get user data. But the problem is that I don't get anything in req.body (undefined), although in req.params I get what I need. Who can suggest what to do?

console.log(req);
if (req.body.password) {
  req.body.password = CryptoJS.AES.encrypt(
    req.body.password,
    process.env.PASS_SEC
  ).toString();
}

try {
  const updatedUser = await User.findByIdAndUpdate(
    req.params.id,
    {
      $set: req.body,
    },
    { new: true }
  );

  res.status(200).json(updatedUser);
} catch (err) {
  res.status(500).json(err);
}
})```


**Here is my request in Insomnia: **

https://imgur.com/a/JiCXaDe
meolex
  • 1
  • 1

2 Answers2

0

Try doing:

npm i body-parser

// then in your app
var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// create application/json parser
var jsonParser = bodyParser.json()


// POST /api/users gets JSON bodies
app.put('/api/users', jsonParser, function (req, res) {
  console.log(req.body);
})
0

I can get data from POST request, but from PUT - no.

enter image description here

meolex
  • 1
  • 1
  • Have you tried to log the whole request to see if there's anything wrong in it ? – liguepk Aug 05 '22 at 10:48
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Y. Gherbi Aug 08 '22 at 09:40