我尝试使用以下命令通过电子邮件查询用户
Meteor.users.findOne({'emails.address': 'me@example.com'});
它在mongo shell中工作,但它在Meteor中返回undefined。
有什么想法吗?
更新
原来我无法查询其他用户。查询登录的用户电子邮件时,相同的查询有效。 那么现在的问题是如何查询所有用户?
答案 0 :(得分:12)
默认情况下,Meteor只发布登录用户,如您所述,您可以针对该用户运行查询。要访问其他用户,您必须在服务器上发布它们:
Meteor.publish("allUsers", function () {
return Meteor.users.find({});
});
在客户端订阅他们:
Meteor.subscribe('allUsers');
另请注意,您可能不想发布所有字段,以便指定要发布/不发布的字段:
return Meteor.users.find({},
{
// specific fields to return
'profile.email': 1,
'profile.name': 1,
'profile.createdAt': 1
});
发布集合后,您可以为所有用户运行查询和访问信息。
答案 1 :(得分:4)
这可能会有所帮助:
var text = "me@example.com";
Meteor.users.findOne({'emails.address': {$regex:text,$options:'i'}});
答案 2 :(得分:1)
首先,您需要按照上述答案发布用户并运行以下命令
Meteor.users.find({"emails": "me@example.com"}).fetch()
OR
Meteor.users.find({"emails.0": "me@example.com"}).fetch()