【问题标题】:Meteor: how to access another user's details?Meteor:如何访问另一个用户的详细信息?
【发布时间】:2016-05-18 20:04:44
【问题描述】:

Meteor.users.findOne() 还给我我的用户文档。

Meteor.users.findOne({_id: 'my ID'}) 还给我我的用户文档。

Meteor.users.findOne({_id: 'another users's ID'}) 还给我 UNDEFINED。

这显然受到安全性的限制。但是我怎样才能访问其他用户的帐户详细信息,例如_id、姓名、个人资料等?

【问题讨论】:

  • 您是否发布了具有该特定 ID 的用户?

标签: meteor meteor-accounts


【解决方案1】:

像这样在服务器上运行它:

服务器:

Meteor.publish("otherUsers", function (userID) {
  return Meteor.users.findOne({_id: userID});
});

客户:

Meteor.subscribe("otherUsers", <userIdYouWantToGetDetailsFor>);

然后您可以在客户端上执行 Meteor.users.findOne 请记住,您只能为您的用户和您在流星订阅中传递的用户 ID 执行此操作

【讨论】:

  • 谢谢!我有出版商,但今晚会检查。
【解决方案2】:

您需要为用户添加发布者。这是一个例子:

// The user fields we are willing to publish.
const USER_FIELDS = {
  username: 1,
  emails: 1,
};

Meteor.publish('singleUser', function (userId) {
  // Make sure userId is a string.
  check(userId, String);

  // Publish a single user - make sure only allowed fields are sent.
  return Meteor.users.find(userId, { fields: USER_FIELDS });
});

然后在客户端你可以像这样subscribe

Metor.subscribe('singleUser', userId);

或像这样使用template subscription

this.subscribe('singleUser', userId);

安全说明:

  1. 始终将check 参数传递给您的发布商,否则客户可能会做坏事,例如将{} 传递给userId。如果您遇到错误,请确保您meteor add check
  2. 始终对用户集合使用fields 选项。否则你会公布他们所有的秘密。请参阅common mistakes 的“已发布的秘密”部分。

【讨论】:

  • 支票包太棒了。我只是从文档中知道
  • 谢谢!我有出版商,但今晚会检查。
  • 如何在模板中引用已发布的用户配置文件?我还需要模板助手吗?
  • 您可以使用常规的findfindOne,例如Meteor.users.findOne(someUserId)。是的,您可能会通过帮助程序将其公开给模板。
猜你喜欢
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多