How to partly update meteor.users.profile?
Asked Answered
O

2

14

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

Optics answered 12/3, 2015 at 14:45 Comment(1)
possible duplicate of Update meteor collection without removing or overriding existing fieldsWot
F
19

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}});
Fraley answered 12/3, 2015 at 16:3 Comment(1)
Answer by @Poinciana is a nice simpler solution Meteor.users.update(id, {$set: {"profile.someNewField": newData}});Keel
P
29

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.

Poinciana answered 22/7, 2015 at 23:48 Comment(0)
F
19

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}});
Fraley answered 12/3, 2015 at 16:3 Comment(1)
Answer by @Poinciana is a nice simpler solution Meteor.users.update(id, {$set: {"profile.someNewField": newData}});Keel

© 2022 - 2024 — McMap. All rights reserved.