【发布时间】:2018-08-15 10:15:18
【问题描述】:
我正在尝试通过为其分配一个在架构中定义的新字段来更新对象,如下所示:
exports.updatePlot = async (req, res) => {
let modifications = {};
modifications = Object.assign(modifications, req.body.props);
const id = modifications.id;
try {
const updatedPlot = await Plot.findByIdAndUpdate(
id,
{ $set: modifications },
{ new: true }
);
console.log(('updated plot saved:', updatedPlot));
res.json({
updatedPlot
});
} catch (e) {
console.log('error', e);
return res.status(422).send({
error: { message: 'e', resend: true }
});
}
};
这在我修改现有字段时有效。但是,当我尝试添加一个新字段(在 Mongoose 模式中定义,但在数据库中的对象中不存在)时,它会失败。意思是,没有错误,但没有添加新字段。
有什么想法吗?
【问题讨论】:
-
我认为你不需要使用 $set。
Plot.findByIdAndUpdate(query,body,options)
标签: javascript node.js express mongoose mongoose-schema