【发布时间】:2018-01-14 08:02:04
【问题描述】:
我下面的代码不允许 API 用户通过传递一个请求属性来仅更新一个字段。我可以删除 userObj 处的空值,但 UI 开发人员必须从数据库中传递现有数据才能进行更新,这不是最佳做法。
这是我的特快路线:
router.put('/user', (req, res) => {
const userObj = {
name: req.body.name || null,
location: {
city: req.body.city || null
},
phone: req.body.phone || null
};
User.updateUser(req.body.id, userObj)
});
这是我的 Mongoose 模型的 updateUser 函数:
module.exports.updateUser = (_id, userObj, callback) => {
User.findOneAndUpdate({_id}, userObj, { upsert: true, 'new': true }, callback);
}
【问题讨论】:
标签: javascript node.js express mongoose ecmascript-6