How to partly update meteor.users.profile?

Michael Hoeller picture Michael Hoeller · Mar 12, 2015 · Viewed 11.2k times · Source

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

Answer

Quin picture Quin · Jul 23, 2015

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.