user1532669
user1532669

Reputation: 2378

Why doesn't this code appear to add record to Meteor.users.profile object?

I'm trying to create a page to contain a profile form. So I've got a text box for a 'name' field and a button to add it.

So in my client code I have:

  Template.profile.events({
     'click input.add': function () {
        Meteor.users.update({_id: this._id}, { $set:{"profile.name": profile_firstname}} )
     }
  });

and in my template I have:

<template name="profile">
{{#if currentUser}}
<div class="profile">
  <h2>Profile</h2>
  <input type="text" id="profile_firstname" />
  <input type="button" class="add" value="Update Profile" />
</div>
{{/if}}
</template>

When I try to find the users on the console I don't find the profile. What else do I need to do get this working?

Upvotes: 0

Views: 50

Answers (1)

Neil
Neil

Reputation: 2147

Try using Meteor.userId() rather than this._id.

Template.profile.events({
  'click input.add': function (e, t) {
    var firstname = t.$('#profile_firstname').val();
    Meteor.users.update({_id: Meteor.userId()}, { $set:{"profile.name": firstname}} );
  }
});

In the click handler, this refers to the data context of templates input element. Alternatively, you could also set the context using #with and your original code would work.

<template name="profile">
{{#if currentUser}}
<div class="profile">
  <h2>Profile</h2>
  {{#with currentUser}}
  <input type="text" id="profile_firstname" />
  <input type="button" class="add" value="Update Profile" />
  {{/with}}
</div>
{{/if}}
</template>

Upvotes: 1

Related Questions