【问题标题】:Node rest API: put request is not updating the request data节点休息 API:放置请求未更新请求数据
【发布时间】:2019-10-19 04:49:55
【问题描述】:

尝试使用 PUT 请求更新数据。但是,数据并没有更新和返回邮递员中以前的数据。

邮递员放置请求:

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

邮递员回复:

{
    "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
}

我也尝试过使用 findByIdAndUpdate。没有得到结果。任何帮助将不胜感激。

控制器:

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.put('/actors/:actorId', Actor.updateActor);

【问题讨论】:

    标签: javascript node.js express mongoose


    【解决方案1】:

    要解决 ObjectId 错误,请使用以下代码。

    var mongoose = require('mongoose');
    const updateActor = await Actor.findOneAndUpdate({"_id":mongoose.Types.ObjectId(actorId)},data, { new: true });
    res.send(updateActor);
    

    【讨论】:

      【解决方案2】:

      您的邮递员请求是http://localhost:3000/api/actors/5daa8f1c5845ad0b5826b8d9?name=Tom,所以看起来要更新的数据将在req.query而不是req.body

      注意:您应该将要更新的数据放在正文中,而不是像您所做的那样查询。

      更多信息here

      【讨论】:

      • 已解决。我有个问题。问题是:如果我想使用 req.body 发送数据,那么程序是什么?
      • 对于邮递员,您可以将数据放在正文部分,如下所示:i.stack.imgur.com/kc3PD.png
      • 知道了。我试过 x-url 编码。因为,通过查询更新数据不是一个好的选择。
      【解决方案3】:
      Please use following code for getting update data 
      
       Actor.findOneAndUpdate({"_id":ObjectId(actorId)},data, 
       { new: true}).then((updatedData) => {
       res.send(updatedData);
       });
      

      【讨论】:

      • 谢谢。响应码是 200。但是,数据库中的数据没有更新。
      • 请检查
      • const { ObjectId } = require('mongodb');添加该行
      • 我尝试了以下方法:Actor.findOneAndUpdate({"_id":mongoose.Types.ObjectId(actorId)}, data, { new: true}).then((updatedData) => { res.send(updatedData); });而且,这行得通。我们需要在 ObjectId 之前添加 mongoose.Types。
      【解决方案4】:
      exports.updateActor = async(req, res, next) => {
          try {
              const actorId = req.params.actorId;
              const data    = req.body;
      
              const updateActor = await Actor.update({"_id":actorId},data);
      
      
              // res.status(200).json({ message: "Data has Updated Successfully!" });
              res.send(updateActor);
      
          } catch (err) {
              res.status(500).json({ message: err.message });
          }
      };
      
      
      try thiss...
      

      【讨论】:

      • 响应码是 200。但是,数据库中的数据没有更新。数据库连接也很好。因为,我可以获取、删除数据。
      猜你喜欢
      • 2022-01-13
      • 1970-01-01
      • 2017-08-09
      • 2015-10-21
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2021-05-07
      • 2019-09-30
      相关资源
      最近更新 更多