How to partly update meteor.users.profile?

旧巷老猫 提交于 2019-11-30 13:20:46

问题


I have started a min app based on meteor boilerplate with the module accounts-ui.

There is a collection created call users one of its elements is profile, this again has an element called "name" which gets the login name.

With in this test app is an option to update a user profile. The data for the update comes from a Form submit. I have attached the event listener here

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

So data has everything from the form. The loginname "name" is not contained in the form so also not in data.

before the update I have users.profile.name -> contains data after the update I have users.profile.* -> * equals everything from the form but "name" is gone.

Finally: who can I keep the profile.name field ? At the end I like to have in users.profile everthing from the from PLUS the "name" filed.

Thanks for any hint, as you read I am new to meteor - and try to understand how things link together.

Michael


回答1:


You're replacing the entire existing profile object with your data object, so anything that was there before, including the name key, is going to be wiped out.

If name is the only thing in profile that you want to keep, just add it to your data object with its own key. That way the new object you place under profile will have a name field that is equivalent to the old one.

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



回答2:


You can easily keep the old profile data while updating the parts you want changed like this:

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

Make sure "profile.someNewField" is in quotes.



来源:https://stackoverflow.com/questions/29012836/how-to-partly-update-meteor-users-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!