繁体   English   中英

将方法放入 Express 给空数组

[英]Put Method In Express Giving Empty Array

**所以,当我创建一个简单的 REST API 并且我正在使用 res.route 并且在其中我正在执行获取请求和放置请求时,但是当我触发放置请求时它什么也不做,并且 Z41BEF06F539B3906B2EE5F0F 只给出一个空字符串7我使用 mongodb 作为数据库和 mongoose enter code here **

app.route("/articles/:ReqTitle")
  .get(function(req, res){
    const RequestedArtical = req.params.ReqTitle;
    Article.findOne({title: RequestedArtical}, function(err, FoundArticle){
      if(FoundArticle){
        res.send(FoundArticle);
      }else{
        res.send("No artice with that title was found");
      }
    });
  }
).put(function(req, res){
    const RequestedArtical = req.params.ReqTitle;
    Article.updateOne({title: RequestedArtical},
      {title: req.body.title, content: req.body.content},                              
      {overwrite: true},
      function(err){
        if(!err){
          res.send("Sucessfulyy updated the article");                                           
        }else{
          res.send(err);
        }
      }
    );
  }  
);

您正在使用{overwrite: true},并且 MongoDB 服务器不允许使用updateOne FYI覆盖文档。 所以,从文档中,如果你使用overwrite: true ,你应该使用replaceOne()

put(function(req, res) {
            const RequestedArtical = req.params.ReqTitle;
            console.log(RequestedArtical);
            Article.replaceOne({
                    title: RequestedArtical
                }, {
                    title: req.body.title,
                    content: req.body.content
                },
                //{overwrite: true},
                                
                function(err) {
                    if (!err) {
                        res.send("Sucessfulyy updated the article");
                    } else {
                        res.send(err);
                    }
                }
            );
        }

update()相同,除了MongoDB用给定文档替换现有文档(no atomic operators like $set)

暂无
暂无

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

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