【发布时间】:2021-01-02 03:21:27
【问题描述】:
我正在尝试使用 findOneAndUpdate() 方法通过 mongoose 更新 mongoDB, 我从 req.body 中解构了我的字段,但是如果我只更新了一个值,其他人设置为 null,我该如何解决这个问题
代码
const { name, email, phone, type } = req.body;
await Contact.findOneAndUpdate(
{ _id: req.params.id },
{ $set: { name, email, type, phone } },
{ upsert: true },
(err, updatedContact) => {
if (err) {
console.error(err.message);
res.status(400).send('Could not updat');
} else {
res.json(updatedContact);
}
}
);
});
******************************************
【问题讨论】:
-
你能澄清你在问什么吗?
-
如何在不将不想更新的值设置为 null 的情况下使用 findOneandUpdate
-
已经找到解决方案,感谢您的努力
标签: node.js mongodb mongoose mern