Meteor:如何访问另一个用户的详细信息?

Meteor: how to access another user's details?

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

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

Meteor.users.findOne({_id: 'another users's ID'}) 返回 UNDEFINED。

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

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

服务器:

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

客户:

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

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

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

// 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 的 "Published Secrets" 部分。