简体   繁体   English

通过邮递员更新猫鼬

[英]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: 在邮递员的PUT命令中,我输入:

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

But it would only prompt a CastError. 但这只会提示CastError。 How should the URL look like if I want to update a document using the PUT command? 如果我想使用PUT命令更新文档,URL应该是什么样?

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

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

For this url: url/doSomething/taskId , you do something like this: 对于此url: url/doSomething/taskId ,您可以执行以下操作:

{
  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 要使用猫鼬,您需要具有如下网址: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 然后在邮递员中,您可以对url / doSomething / taskId / name发出放置请求,现在您可以使用req.params.taskId

if you want to supply the data as /url/doSomething?taskId=&name= you would need to use req.query.taskId 如果要以/ url / doSomething?taskId =&name =的形式提供数据,则需要使用req.query.taskId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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