在更新数据库对象的特定领域|爱可信,终极版的形式

问题描述 投票:0回答:1

我有一个对象称为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对象更新的特定领域。如果需要的话我可以给更多的代码。

谢谢阅读!

javascript server put redux-form axios
1个回答
1
投票
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!' });
        });

    });
});
© www.soinside.com 2019 - 2024. All rights reserved.