简体   繁体   中英

Updating in Mongoose thru Postman

In my mongoose controller, I have something like:

exports.update_a_task = function(req, res) {
  Task.findOneAndUpdate({_id: req.params.taskId}, req.body, {new: true}, function(err, task) {
    if (err)
      res.send(err);
    res.json(task);
  });
};

And in my PUT command in Postman I put:

url/doSomething/taskId/name //or ,name

But it would only prompt a CastError. How should the URL look like if I want to update a document using the PUT command?

如果taskId和name是查询参数,则url变为

url/doSomething/?taskId=123&name=name

For this url: url/doSomething/taskId , you do something like this:

{
  name: "the_name_you_want"
}

Inside of Postman.

To get it in mongoose you need to have the url like this url/doSomething/:taskId/:name

then in postman you can do a put request to url/doSomething/taskId/name now you can use req.params.taskId

if you want to supply the data as /url/doSomething?taskId=&name= you would need to use req.query.taskId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM