【发布时间】:2017-12-13 19:56:21
【问题描述】:
我正在尝试使用此代码查找和更新:
exports.updatePlot = async (req, res) => {
let modifications = {};
modifications.name = req.body.name;
modifications.grower = req.body.props.grower;
modifications.variety = req.body.props.variety;
modifications.planted = req.body.props.planted;
const id = req.body.props.id;
try {
const updatedPlot = await Plot.findByIdAndUpdate(
id,
{ $set: { modifications } },
{ new: true }
);
res.json({
updatedPlot
});
} catch (e) {
return res.status(422).send({
error: { message: 'e', resend: true }
});
}
};
我可以看到我的请求正文包含数据,我还可以看到 Mongoose 正在查找对象,但没有更新它,因为这是它作为更新图记录的内容:
{"_id":"id string here","name":"old name",...all other properties...}
我猜我的请求格式不正确?有人看到我的错误吗?
架构如下所示:
const plotSchema = new Schema(
{
name: String,
...other properties...
},
{ bufferCommands: false }
);
const ModelClass = mongoose.model('plot', plotSchema);
【问题讨论】:
标签: node.js mongodb mongoose mongoose-schema