Meteor:如何访问另一个用户';什么是细节?
Meteor:如何访问另一个用户';什么是细节?,meteor,meteor-accounts,Meteor,Meteor Accounts,Meteor.users.findOne()返回我的用户文档 Meteor.users.findOne({u-id:'my-id'})返回我的用户文档 Meteor.users.findOne({u id:'另一个用户的id'})返回未定义的内容 这显然受到安全的限制。但是我如何访问其他用户的帐户详细信息,例如id、姓名、配置文件等?在服务器上运行它,如下所示: 服务器: Meteor.publish("otherUsers", function (userID) { return Mete
Meteor.users.findOne()
返回我的用户文档
Meteor.users.findOne({u-id:'my-id'})
返回我的用户文档
Meteor.users.findOne({u id:'另一个用户的id'})
返回未定义的内容
这显然受到安全的限制。但是我如何访问其他用户的帐户详细信息,例如id、姓名、配置文件等?在服务器上运行它,如下所示: 服务器:
Meteor.publish("otherUsers", function (userID) {
return Meteor.users.findOne({_id: userID});
});
客户:
Meteor.subscribe("otherUsers", <userIdYouWantToGetDetailsFor>);
Meteor.subscribe(“其他用户”);
然后,您可以在客户端执行Meteor.users.findOne请记住,您只能为您的用户以及您在Meteor订阅中传递的用户ID执行此操作您需要为用户添加发布者。下面是一个例子:
//我们愿意发布的用户字段。
const USER_字段={
用户名:1,
电邮:1,
};
Meteor.publish('singleUser',函数(userId){
//确保userId是一个字符串。
检查(用户ID、字符串);
//发布单个用户-确保只发送允许的字段。
返回Meteor.users.find(userId,{fields:USER\u fields});
});
然后在客户端上,您可以这样做:
Metor.subscribe('singleUser',userId);
或者像这样使用:
this.subscribe('singleUser',userId);
安全说明:
检查发布者的参数,否则客户端可能会对用户ID执行错误操作,如传递{}
。如果出现错误,请确保执行流星添加检查
字段
选项。否则你会公布他们所有的秘密。请参阅的“公开的秘密”部分您是否发布了具有该特定ID的用户?检查包太棒了。我只是从文件上知道的谢谢!我有发布者,但今晚将进行检查。如何在模板中引用已发布的用户配置文件?我还需要一个模板助手吗?你需要做一个常规的
find
或findOne
比如Meteor.users.findOne(someUserId)
。是的,您可能会通过助手将其暴露给模板。谢谢!我有出版商,但今晚会查收的。