为什么这段代码似乎没有向Meteor.users.profile对象添加记录?

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

本文关键字:users Meteor profile 对象 加记录 添加 段代码 代码 为什么      更新时间:2023-09-26

我试图创建一个包含配置文件表单的页面。所以我有一个'name'字段的文本框和一个添加它的按钮。

在我的客户端代码中,我有:

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

在我的模板中我有:

<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>

当我试图在控制台上找到用户时,我找不到配置文件。我还需要做什么才能使它工作?

尝试使用Meteor.userId()而不是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}} );
  }
});

在click处理程序中,this为模板输入元素的数据上下文。或者,您也可以使用#with设置上下文,这样您的原始代码就可以工作了。

<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>