如何部分更新meteor.users.profile?

How to partly update meteor.users.profile?

我已经启动了一个基于 meteor 样板的最小应用程序,模块为 accounts-ui。

创建了一个名为 users 的集合,其元素之一是配置文件,这又包含一个名为 "name" 的元素,它获取登录名。

此测试应用中有一个更新用户个人资料的选项。更新数据来自表单提交。我在这里附加了事件监听器

Template.profile.events({
  'submit form': function(event) {
    event.preventDefault();
    var data = SimpleForm.processForm(event.target);
    Meteor.users.update(Meteor.userId(), {$set: {profile: data}});
  }
});

因此,数据拥有表格中的一切。登录名 "name" 不包含在表单中,因此也不包含在数据中。

更新前我有 users.profile.name -> 包含数据 更新后我有 users.profile.* -> * 等于表单中的所有内容,但 "name" 消失了。

最后:谁可以保留 profile.name 字段?最后,我喜欢 users.profile 来自 PLUS 的所有内容以及 "name" 归档。

感谢您的任何提示,正如您阅读的那样,我是 meteor 的新手 - 并尝试了解事物 link 是如何组合在一起的。

迈克尔

您将用您的数据对象替换整个现有配置文件对象,因此之前存在的所有内容(包括名称键)都将被清除。

如果名称是配置文件中您想要保留的唯一内容,只需将其添加到您的数据对象中并使用其自己的键即可。这样,您放置在配置文件下的新对象将有一个与旧对象相同的名称字段。

var data = SimpleForm.processForm(event.target);
data.name = Meteor.user().profile.name;
Meteor.users.update(Meteor.userId(), {$set: {profile: data}});

您可以轻松地保留旧的配置文件数据,同时更新您想要更改的部分,如下所示:

Meteor.users.update(id, {$set: {"profile.someNewField": newData}});

确保 "profile.someNewField" 包含在引号中。