0

I'm creating new users with the following:

Accounts.createUser({
    username: t.find('#username').value,
    password: t.find('#password').value,
    email: t.find('#email').value,
    profile : {
      name: t.find('#name').value,
      division: 'none',
      enrolled: 'false'
    }
  });

I need to be able to update the division field when a user choose from a drop down select. This is my template:

    <template name="userProfile">
  <p>Select your division</p>
  <select id="division">
    <option>Div1</option>
    <option>Div2</option>
    <option>Div3</option>
    <option>Div4</option>
  </select>
  <button id="enrolled"> Not enrolled </button>

  <p>a: {{currentUser.profile.name}}</p>
  <p>b: {{currentUser.profile.division}}</p>
</template>

I have a click event which works, but what I can't figure out is how to append or modify fields in the profiles:

Template.userProfile.events({
    'click #division': function(e, t) {
      console.log('Selected division is: ', t.find('#division').value);
      Meteor.user.profile.division = t.find('#division').value;
    },
    'click: #enrolled': function(e, t) {
      console.log('User is enrolled? : ');
    }
  });

What I'd like to happen is when a user chooses a division the field in the currentUser's profile gets updated.

I have one more question but I'll put that in a separate thread.

Thanks.

Tortex
  • 163
  • 1
  • 1
  • 12

1 Answers1

0

The code shown will only update a local copy.

To make a permanent change requires a database write to the users collection.

Per the documentation for Meteor.users:

Users are by default allowed to specify their own profile field with Accounts.createUser and modify it with Meteor.users.update. To allow users to edit additional fields, use Meteor.users.allow.

If you can't get that working on the client, it is probably a matter of permissions which are enforced on the client and not on the server. Meteor.allow() specifies these permissions on the client, and anything is permitted on the server.

Exploiting that latter fact, another way to implement profile or user information updates is to use remote methods where Meteor.call() on the client sends information to be executed on the server in a function specified in Meteor.Methods(). The server function that you write and put in Meteor.Methods would then test the validity of the update according to any conditions that you might want to defined, and if valid, call Meteor.users.update() with the update to the profile.

Paul
  • 26,170
  • 12
  • 85
  • 119
  • Ahh, that's good to know and gives me something else to learn about :) – Tortex May 26 '14 at 20:46
  • I've tested this on the meteor servers and adding accounts is working fine. Is that what you meant? – Tortex May 26 '14 at 21:11
  • I expanded my answer. Does this help? Have you solved your problem? – Paul May 26 '14 at 23:37
  • It adds more which is great but right now my main issue is I'm unable to get the value of 'enrolled' in my user's profile. I want to be able to get that value, once I can do that I'll figure out how to update it via the server. – Tortex May 27 '14 at 07:49
  • That seems to be a different question. But... might I suggest using a checkbox instead of button for enrolled? – Paul May 27 '14 at 07:52
  • It is! Time for a coffee and 10 minutes away from the keyboard ;-) – Tortex May 27 '14 at 08:04