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

时间:2015-03-12 14:45:11

标签: meteor meteor-accounts

我已经开始了一个基于meteor样板的min应用程序和模块accounts-ui。

有一个集合创建了调用用户其中一个元素是profile,这又有一个名为" name"获取登录名。

在此测试应用程序中,可以选择更新用户配置文件。更新的数据来自表单提交。我在这里附上了事件监听器

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

所以数据包含表格中的所有内容。登录名"名称"不包含在表格中,也不包含在数据中。

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

最后:谁可以保留profile.name字段?最后,我喜欢在users.profile中加入来自PLUS" name"于此。

感谢任何提示,因为你读到我是流星的新手 - 并尝试理解事物是如何联系在一起的。

迈克尔

2 个答案:

答案 0 :(得分:28)

您可以轻松保留旧的个人资料数据,同时更新您想要更改的部分,如下所示:

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

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

答案 1 :(得分:19)

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

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

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