我有一个对象称为item
:
我也有一个PUT请求前人的精力能够改变一些值item
内。有些值是对象的内部,有些则没有。一个例子是,我需要能够改变settings
OBJ内的值。目前我PUT请求是这样的:
export function updateSettings(id, item) {
return dispatch => {
axios
.put(`${settings.hostname}/cookie/${id}`, item)
.then(res => {
dispatch({
type: 'COOKIES_UPDATED',
payload: {
item
}
})
})
.catch(err => console.log(err))
}
}
而res.data
登录时,我只得到这个:_id: "598960d891202556389cxd"
下面是后端proccess PUT请求:
r.put('/cookies' + '/:id', async (ctx, next) => {
console.log(key)
await ctx.db
.replaceOne({ _id: objectId(ctx.params.id) }, ctx.request.body)
ctx.body = { _id: ctx.params.id }
console.log(ctx.request.body)
})
我使用的终极版形式提交我的输入字段和我onSubmit
看起来是这样的:
onSubmit={handleSubmit(props => {
// props is the updated form values
onSubmit(id, props)
})}
这发出了新的价值观到服务器,并更新数据库,但它也与我item
对象的结构打乱。有些领域越来越去除,所以我就提示要使用diff
.Diff
的(https://github.com/srcagency/object-diff)的目的是,如果更改服务器,但对我来说,它doesen't任何更新只发送。 Item
看起来exacly相同。
所以我的问题,我不知道怎样在我item
对象更新的特定领域。如果需要的话我可以给更多的代码。
谢谢阅读!
router.route('/cookies/:id')
.put(function(req, res) {
Items.findById(req.params.id, function(err, item) {
if (err)
res.send(err);
// if you want update all settings object
item.settings = req.body;
// if something on settings field, item.settings.field1 = req.body.field1
item.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Your item updated!' });
});
});
});