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
      ]
    })

并且不能对 Person 进行任何修复。使用 console.log 我可以看到所有作品,但看不到推送。

4

2 回答 2

1

这现在对我有用。谢谢彼得里昂

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')
         }
       });
于 2013-10-05T18:51:21.103 回答
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");
      }
    }
  )
}
于 2013-10-04T06:39:06.640 回答