gpt4 book ai didi

Meteor:如何访问其他用户的详细信息?

转载 作者:行者123 更新时间:2023-12-02 05:47:14 25 4
gpt4 key购买 nike

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

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

Meteor.users.findOne({_id: '另一个用户的 ID'}) 给我返回 UNDEFINED。

这显然是受到安全限制的。但是我如何访问其他用户的帐户详细信息,例如_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. 始终检查发布商的参数,否则客户端可能会做坏事,例如将 {} 传递给 userId。如果出现错误,请确保meteor添加检查
  2. 始终对用户集合使用fields 选项。否则你就会公开他们的所有 secret 。请参阅 common mistakes 的“已发布的 secret ”部分.

关于Meteor:如何访问其他用户的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com