Home > database >  express deprecated res.send(status, body): Use res.status(status).send(body) instead
express deprecated res.send(status, body): Use res.status(status).send(body) instead

Time:10-17

is anything I am doing wrong because I am new to this, I got express deprecated res.send(status, body): Use res.status(status).send(body) instead error when I carry out the update operation using PUT in postman.

router.put("/admin/update_profile/:id", async (req, res) => {
  try {
    const id = req.params.id;
    const updatedData = req.body;
    const options = { new: true };

    const result = await SomeModel.findByIdAndUpdate(id, updatedData, options);

    res.send(result);
    // res.status(200).send(result)
  } catch (err) {
    res.send("Error ", err);
    // res.status(404).send("Error ", err)
  }
});

error snap

CodePudding user response:

Pretty Much what the error says. Express has deprecated res.send(status, body);. Replace that line with res.status(status).send(body);

Please note that writing only res.send(body); will send a status of 200 by default, so remember to use .status() in failure cases.

CodePudding user response:

res.send("Error ", err);

You're passing two parameters to res.send() which as the warning message indicates is deprecated but still supported.

It's interpreting the string literal "Error " as a response status code which is not valid.

You should only pass a single parameter to send() and set the status via status()

res.status(500).send(err);

CodePudding user response:

router.put("/admin/update_profile/:id", async (req, res) => {
try {
   const id = req.params.id;
   const updatedData = req.body;
   const options = { new: true };

   const result = await SomeModel.findByIdAndUpdate(id.trim(), updatedData, 
   options);

     res.status(200).send(result);

 } catch (err) {
       res.status(404).send(err);
 }
});
  • Related