【问题标题】:Meteor: Appending data to a users profileMeteor:将数据附加到用户配置文件
【发布时间】:2014-05-26 20:09:14
【问题描述】:

我正在使用以下内容创建新用户:

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

当用户从下拉选择中选择时,我需要能够更新部门字段。这是我的模板:

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

我有一个有效的点击事件,但我不知道如何在配置文件中附加或修改字段:

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? : ');
    }
  });

我希望当用户选择一个部门时,当前用户的个人资料中的字段会更新。

我还有一个问题,但我会把它放在一个单独的线程中。

谢谢。

【问题讨论】:

    标签: meteor profile account


    【解决方案1】:

    显示的代码只会更新本地副本。

    要进行永久更改,需要将数据库写入用户集合。

    根据documentation for Meteor.users

    默认情况下,允许用户指定自己的个人资料字段 Accounts.createUser 并使用 Meteor.users.update 对其进行修改。允许 用户要编辑其他字段,请使用 Meteor.users.allow。

    如果您无法在客户端上运行,则可能是在客户端而不是服务器上强制执行的权限问题。 Meteor.allow() 在客户端指定这些权限,在服务器上允许任何事情。

    利用后一个事实,实现配置文件或用户信息更新的另一种方法是使用remote methods,其中客户端上的 Meteor.call() 在 Meteor.Methods() 中指定的函数中发送要在服务器上执行的信息.然后,您编写并放入 Meteor.Methods 的服务器函数将根据您可能想要定义的任何条件测试更新的有效性,如果有效,则调用 Meteor.users.update() 更新配置文件。

    【讨论】:

    • 啊,很高兴知道这件事,让我有更多的东西可以学习:)
    • 我已经在流星服务器上进行了测试,添加帐户工作正常。你是这个意思吗?
    • 我扩展了我的答案。这有帮助吗?你的问题解决了吗?
    • 它增加了更多,这很好,但现在我的主要问题是我无法在我的用户个人资料中获得“已注册”的值。我希望能够获得该值,一旦我能够做到这一点,我将弄清楚如何通过服务器对其进行更新。
    • 这似乎是一个不同的问题。但是...我可以建议使用复选框而不是按钮进行注册吗?
    猜你喜欢
    • 2020-02-04
    • 2014-07-27
    • 2017-12-01
    • 2013-05-28
    • 2014-06-05
    • 2015-12-09
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多