JavaScript Node rest API: put request is not updating the request data node rest request,node js rest request,node send rest

Trying to update data using PUT request. But, the data is not updating and returning the previous data in postman.

Postman put request:

http://localhost:3000/api/actors/5daa8f1c5845ad0b5826b8d9?name=Tom

Postman response:

{
    "createdAt": "2019-10-19T04:16:13.317Z",
    "updatedAt": "2019-10-19T04:16:13.317Z",
    "_id": "5daa8f1c5845ad0b5826b8d9",
    "name": "scarlett johansson",
    "birthday": "1980-10-14T00:00:00.000Z",
    "country": "usa",
    "__v": 0
}

I have also tried to use findByIdAndUpdate. Didn't get the result. Any help would be appreciated.

Controller:

exports.updateActor = async(req, res, next) => {
    try {
        const actorId = req.params.actorId;
        const data    = req.body;

        const updateActor = await Actor.findById(actorId);

        updateActor.set(data);

        const actor = await updateActor.save();

        // res.status(200).json({ message: "Data has Updated Successfully!" });
        res.send(actor);

    } catch (err) {
        res.status(500).json({ message: err.message });
    }
};

Router:

router.put('/actors/:actorId', Actor.updateActor);
Answer:1

Your postman request is http://localhost:3000/api/actors/5daa8f1c5845ad0b5826b8d9?name=Tom so look like the data to update will be in req.query instead of req.body.

Note: You should put the data to update in the body instead of query like you're doing.

More info here.

Answer:2

To resolve the ObjectId error use the below code.

var mongoose = require('mongoose');
const updateActor = await Actor.findOneAndUpdate({"_id":mongoose.Types.ObjectId(actorId)},data, { new: true });
res.send(updateActor);
Answer:3
Please use following code for getting update data 

 Actor.findOneAndUpdate({"_id":ObjectId(actorId)},data, 
 { new: true}).then((updatedData) => {
 res.send(updatedData);
 });
Answer:4

I'm using the radio slider as given here: Radio Slider. I want to put a dropdown on the right side of this slider, which I'm doing using display:inline-block like this: <div class="wrapper ...

I'm using the radio slider as given here: Radio Slider. I want to put a dropdown on the right side of this slider, which I'm doing using display:inline-block like this: <div class="wrapper ...

  1. find side using side and angle

I have an algorithm that examines an array of Event (StartTime,EndTime) on a timeline that runs from Midnight->Midnight to check for any gaps. The algorithm works, but there is one configuration where ...

I have an algorithm that examines an array of Event (StartTime,EndTime) on a timeline that runs from Midnight->Midnight to check for any gaps. The algorithm works, but there is one configuration where ...

I have two ajax calls that are using GET, each one having a different success function. I want to wait for the response of both requests and the execution of both success functions and then I need to ...

I have two ajax calls that are using GET, each one having a different success function. I want to wait for the response of both requests and the execution of both success functions and then I need to ...

  1. wait execution ajax javascript
  2. wait for ajax execution

I am using an object to store state and the object looks like this: { "CPC": 0, "NDP": 0, "LPC": 0, "GPC": 1, "PPC": 2 } Where all the key values are an enumerator of a generic type <T&...

I am using an object to store state and the object looks like this: { "CPC": 0, "NDP": 0, "LPC": 0, "GPC": 1, "PPC": 2 } Where all the key values are an enumerator of a generic type <T&...

  1. total number of objects in an area
  2. total number of objects in active directory
  3. total number of objects in a database
  4. total number of objects in s3 bucket
  5. total number of objects expired tsm
  6. total number of objects in a set
  7. total number of objects in a database 32 768
  8. total number of objects in a database access
  9. total number of objects expired
  10. total number of standard objects in salesforce
  11. find total number of objects in active directory
  12. get total number of objects in json
  13. total number of methods in object class in java
  14. tsm total number of objects inspected
  15. total number of objects inspected
  16. total number of objects encrypted
  17. the total number of objects