这是我的控制器,一个表格在这里发送数据:
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 :(得分:1)
这对我有用。谢谢Peter Lyons
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')
}
});
答案 1 :(得分: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");
}
}
)
}