【问题标题】:Meteor.user() only returns _id and usernameMeteor.user() 只返回 _id 和用户名
【发布时间】:2015-02-16 17:01:15
【问题描述】:

使用accounts-passwordaccounts-ui 模块创建用户后,当我在控制台中调用Meteor.user() 时,我没有得到完整的配置文件,只有_idusername,即使有有关文档的更多信息,例如 createdAt 记录。

如果我使用这些模板,也会出现同样的问题:

<template name="user_list">
    <div class="user-list">
    <h1>Users</h1>
        <ul>
        {{#each allUsers}}
            {{> user_item}}
        {{/each}}
        </ul>
    </div>
</template>

<template name="user_item">
    <li class="user-item">
        <p><b>{{username}}</b></p>
        <p>Created: {{createdAt}}</p>
    </li>
</template>

...由这个助手支持:

Template.user_list.helpers({
  allUsers: function () {
    return Meteor.users.find({});
  }
});

在这种情况下,user_list 模板可以很好地遍历用户,并显示用户名,但 createdAt 帮助程序失败,即使我通过 meteor mongo 记录肯定存在。我没有删除自动发布;我的设置仍然完全开放。为什么我无法访问其余的用户记录?

【问题讨论】:

标签: meteor meteor-accounts


【解决方案1】:

默认情况下,accounts 包只发布 id、username 和 profile 字段。要发布更多内容,请将此“通用”发布添加到服务器:

Meteor.publish(null, function() {

  if (this.userId != null) {
    return Meteor.users.find({
      _id: this.userId
    }, {
      fields: {
        'createdAt': 1,
      }
    });
  } else {
    return this.ready();
  }
});

注意:即使没有用户确保与 Spiderable 包的兼容性,它也会返回 this.ready()

【讨论】:

  • 为什么不只拥有Meteor.publish(null, function() {return Meteor.users.find({_id: this.userId}, { fields: { 'fieldToPublish': 1 } }) });。你不能在 mongodb 上将 _id 作为 null,因此不需要条件检查。
  • @Akshat,这会向数据库发送一个额外的(不必要的)查询,不是吗?但是,发布不是反应式的,所以我认为这不会那么好。
  • @tarmes 到目前为止,我已经读到发布的集合需要由客户端订阅。在这种情况下有必要吗?如果我打电话给return Meteor.user().createdAt;,我仍然会收到一个未定义的错误。但是,如果我用Meteor.user() 调用当前用户的信息,我现在会得到当前用户的createdAt
  • 您通常需要订阅发布,但“通用”发布(通过传递“null”创建)是一个例外。如果 Meteor.user() 现在正在返回信息,那么您的代码中的 Meteor.user().createdAt 一定有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多