我得到了[object Object]9778177结果,我试图解析价值,但也没有帮助,有些事情是错的.
let x = [{
"total_count": 7
}, {
"total_count": 9
}, {
"total_count": 778
}, {
"total_count": 177
}]
let sum = x.reduce((accum, obj) => {
return accum + obj.total_count
})
console.log(sum)
Run Code Online (Sandbox Code Playgroud) 我下面的代码不允许API用户通过传递一个请求属性来仅更新一个字段。我可以在处删除null userObj,但是UI开发人员将必须从数据库传递现有数据来进行更新,这不是最佳实践。
这是我的Express路线:
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)
});
Run Code Online (Sandbox Code Playgroud)
这是我的猫鼬模型的updateUser功能:
module.exports.updateUser = (_id, userObj, callback) => {
User.findOneAndUpdate({_id}, userObj, { upsert: true, 'new': true }, callback);
}
Run Code Online (Sandbox Code Playgroud)