Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Meteor中的动态对象更新user.profile_Javascript_Meteor - Fatal编程技术网

Javascript 使用Meteor中的动态对象更新user.profile

Javascript 使用Meteor中的动态对象更新user.profile,javascript,meteor,Javascript,Meteor,因此,我试图实现的是更新user.profile,保持user.profile中已经存在未更新的旧数据 因此,我最初的user.profile包含以下内容: { accountType: 'Student', xyz: 'something', etc... } { 'xyz': 'something else', 'bar': 'bar', etc... } 在我的更新方法中,如果不需要更新,我希望保留这些值,因此如果我希望添加以下内容: { accountTy

因此,我试图实现的是更新
user.profile
,保持
user.profile
中已经存在未更新的旧数据

因此,我最初的
user.profile
包含以下内容:

{
  accountType: 'Student',
  xyz: 'something',
  etc...
}
{
  'xyz': 'something else',
  'bar': 'bar',
  etc...
}
在我的更新方法中,如果不需要更新,我希望保留这些值,因此如果我希望添加以下内容:

{
  accountType: 'Student',
  xyz: 'something',
  etc...
}
{
  'xyz': 'something else',
  'bar': 'bar',
  etc...
}
我希望看到对象合并和更新后的更新配置文件

我尝试使用的是
update
upsert
,但在这两种情况下,以及我尝试更新
用户配置文件时的所有测试中,旧数据都会被新数据完全替换

以下是我最近的一次尝试:

Meteor.users.update(this.userId, {
  $set: {
    profile: data
  }
},
{ upsert: true });
但我也试过:

Meteor.users.upsert(this.userId, {
  $set: {
    profile: data
  }
});
我怎样才能实现我所需要的?感谢来自以下网站的

$set运算符用指定的值替换字段的值

因此,当您将其更新为
{$set:{profile:…}}
时,它将替换整个
profile
文档

您应该这样使用它:

$set: {
  'profile.<field_name>': '<field_value>',
  ...
}

太棒了,我非常喜欢这个解决方案。它很有魅力!干杯