Michael Hoeller
Michael Hoeller

Reputation: 24188

How to partly update meteor.users.profile?

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

Upvotes: 14

Views: 11399

Answers (2)

Quin
Quin

Reputation: 541

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.

Upvotes: 29

RevMen
RevMen

Reputation: 563

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}});

Upvotes: 19

Related Questions