如何修复“ model.findByIdAndUpdate”不更新mongodb 4.0中的文档?

时间:2019-07-28 03:04:05

标签: mongodb express mongoose mongodb-query

在终端中输入以下命令后,我正在学习expressjs,并尝试通过id更新mongodb数据库中的文档并使用PUT指定路由:

curl -X PUT --data "name =James&age = 20&nationality=American"http://localhost:3000/people/5d3ba2a863ba682d70242131

它打印出文档,但没有任何更新

我按照默认设置({new:false})的建议使用了{new:true},但是什么都没有改变。没有指定任何可行的路线 例如

Person.findOneAndUpdate({name:'deean'},{age:34},function(err,response){
   console.log(response);
}); 

以上代码有效 但这不是:

app.put('/people/:id', function(req, res){
   Person.findByIdAndUpdate(req.params.id, req.body, {new: true} ,function(err, response){
      if(err) 
       res.json({message:"Error in updating person with id" + req.params.id});
      res.json(response);
      });
});
app.listen(3000);

我要更改的原始文档是

{ _id: 5d3ba2a863ba682d70242131,
name: 'deean ajashi',
age: 34,
nationality: 'indian',
__v: 0 } ]

这是我在终端上得到的输出

    C:\Users\Diola>curl -X PUT --data "name = James&age = 20&nationality = American" http://localhost:3000/people/5d3ba2a863ba682d70242131

{"_id":"5d3ba2a863ba682d70242131","name":"dean ajashi","age":34,"nationality":"indian","__v":0}
C:\Users\Diola>

我使用的格式来自Tutorialspoint click to check out page on Tutorialspoint

编辑:我不确定这是您通过型号和路由器文件表示的意思

var express = require('express');
var multer = require("multer");
var upload = multer(); 
var app = express();
app.set('view engine', 'pug');
app.set('views', './views');
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 

app.use(upload.array()); 
app.use(express.static('public')); 
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb', { useNewUrlParser: true }); 
var personSchema = mongoose.Schema({
   name: String,
   age: Number,
   nationality: String 
});
var Person = mongoose.model("Person", personSchema);
app.get('/person', function(req, res){
   res.render('person'); 
});
app.post('/person', function(req, res){ 
   var personInfo = req.body; 
   console.log('recived your request'); 
   console.log(req.body);
   
   
   if(!personInfo.name || !personInfo.age || !personInfo.nationality){ 
      res.render('show_message', {
         message: "Sorry, you provided worng info", type: "error"});
   } else {
      var newPerson = new Person({  
         name: personInfo.name,
         age: personInfo.age,
         nationality: personInfo.nationality
      });

		newPerson.save(function(err, Person){ 
         if(err)
            res.render('show_message', {message: "Database error", type: "error"});
         else
            res.render('show_message', {
               message: "New person added", type: "success", person: personInfo});
      });
   }
});

1 个答案:

答案 0 :(得分:0)

当您传递{new: true}时,它返回错误的更新文档( Vs )旧文档,如果它为假,则与更新操作无关,您能给我们您的模型和路由器文件吗?同时-尝试下面的代码并通过打印req.params.idreq.body来检查是否能够获得请求:

app.put('/people/:id', function(req, res){
   Person.findByIdAndUpdate(req.params.id, {$set:req.body}, {new: true} ,function(err, response){
       if(err) res.json({message: "Error in deleting record id " + req.params.id});
       else res.json(response);
      });
});
app.listen(3000);