2013-10-04 97 views
1

這是我的控制器,一種形式在這裏發送的數據:我不能推數組貓鼬架構

exports.addrepair = function(req, res, next){ 
     Person.findById(req.body.id, function (err, Person) { 
       Person.update({id: req.body.id}, 
           {$pushAll: { 
             problem: req.body.problem 
            , solution: req.body.solution 
            , date_on: Date.now() 
            }} 
       ,{upsert:true},function(err){ 
        if(err){ 
          console.log(err); 
        }else{ 
          console.log("Added"); 
        } 
       }) 
     }) 
    } 

的模式是:

var Person = new Schema ({ 
     name: String, 
     Repair: [ 
     problem: String, 
     solution: String, 
     date_on: Date 
     ] 
    }) 

,並不能推動任何維修人員。隨着console.log我可以看到所有的作品,但不是推。

回答

0
exports.addrepair = function(req, res, next) { 
    //The Person.findById you had here was unnecessary/useless 
    Person.findByIdAndUpdate(req.body.id, 
    { 
     Repair: { 
     $push: { 
      problem: req.body.problem, 
      solution: req.body.solution, 
      date_on: Date.now() 
     } 
     } 
    }, 
    //You don't want an upsert here 
    function(err){ 
     if(err){ 
     console.log(err); 
     }else{ 
     console.log("Added"); 
     } 
    } 
) 
} 
+0

Thaks,但不工作,只推第一次也是唯一創建德ID不是問題不解決,而不是去date_on –

+1

好吧,讓我們來談談細節。你發佈的模式甚至不是有效的JavaScript(你可能忽略了Repair數組中的花括號)。您要小心謹慎地發佈您實際運行的切合實際的片段,也許我會盡力爲您提供更多幫助。 –

1

這適用於我現在。 Thaks彼得·萊昂斯

Person.findByIdAndUpdate(req.body.id,       
     { $push: 
     { repair: 
      { problem: req.body.problem 
      , solution: req.body.solution 
      , date_on: Date.now() 
      } 
     }, 
     function(err){ if(err){ 
      console.log(err) 
     } else { 
      console.log('Added') 
     } 
     });