2015-04-28 51 views

回答

0

update方法用於尋找和不返回已更新的文件更新文件。基本上你在做的是找到文件而不更新它們,因爲update函數的第一個參數是搜索條件。在更新屬性後,您需要使用save函數來更新退出的文檔。下面

你的代碼,修改(未測試):

//PUT to update a blob by ID 
.put(function(req, res) { 
    //find the document by ID 
    mongoose.model('Email').findById(req.id, function (err, email) { 
     //add some logic to handle err 
     if (email) { 
      // Get our REST or form values. These rely on the "name" attributes 
      email.email = req.body.email; 
      email.password = req.body.password; 
      email.servico = req.body.servico; 

      //save the updated document 
      email.save(function (err) { 
       if (err) { 
        res.send("There was a problem updating the information to the database: " + err); 
       } 
       else { 
        //HTML responds by going back to the page or you can be fancy and create a new view that shows a success page. 
        res.format({ 
         html: function(){ 
          res.redirect("/emails"); 
         }, 
         //JSON responds showing the updated values 
         json: function(){ 
          res.json(email); 
         } 
        }); 
       } 
      }); 
     } 
    }); 
}) 
+0

工作就像一個魅力!感謝您的解釋! – tiansial