backbone客户端更新模型属性:
ocase.save({ currentStep : theStep },callback);服务端mongoose代码:
OCase.findByIdAndUpdate(req.params.id,
req.body, callback);它在mongodb2.6上工作得很好,但在mongodb2.4上就不行了,错误是:
update err:MongoError: exception: Mod on _id not allowed所以我试着去掉"_id",只保存其他属性:
OCase.findByIdAndUpdate(req.params.id,
{subject : req.body.subject,
description : req.body.description
},callback);然后我得到了另一个错误:
update err:TypeError: Cannot read property '_id' of undefined我真的很困惑,我现在该怎么办?
最后,我必须先查询(保存)文档,然后调用“findById”方法进行更新。
OCase.findById(req.params.id,
function(err,ocase){
ocase.set(req.body);
ocase.save(function(err,ocase){
res.send(ocase);
});
});发布于 2015-01-24 02:05:32
通过添加$set运算符来解决它:
OCase.findByIdAndUpdate(req.params.id, {
$set: {
subject: req.body.subject,
description: req.body.description,
currentStep: req.body.currentStep
}
}, callback);https://stackoverflow.com/questions/28104325
复制相似问题