【发布时间】:2015-11-08 04:23:13
【问题描述】:
我正在尝试使用以下命令通过电子邮件查询用户
Meteor.users.findOne({'emails.address': 'me@example.com'});
它在 mongo shell 中工作,但在 Meteor 中返回 undefined。
有什么想法吗?
更新
原来我无法查询其他用户。当我查询登录的用户电子邮件时,相同的查询有效。 那么现在的问题是如何查询所有用户?
【问题讨论】:
我正在尝试使用以下命令通过电子邮件查询用户
Meteor.users.findOne({'emails.address': 'me@example.com'});
它在 mongo shell 中工作,但在 Meteor 中返回 undefined。
有什么想法吗?
更新
原来我无法查询其他用户。当我查询登录的用户电子邮件时,相同的查询有效。 那么现在的问题是如何查询所有用户?
【问题讨论】:
默认情况下,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
});
发布集合后,您可以为所有用户运行查询和访问信息。
【讨论】:
Meteor.users.find({'profile.isAdmin':true}).toArray();的身份访问该集合
这可能会有所帮助:
var text = "me@example.com";
Meteor.users.findOne({'emails.address': {$regex:text,$options:'i'}});
【讨论】:
bob+1@example.com 的用户。
首先你需要发布上面提到的用户答案并运行以下命令
Meteor.users.find({"emails": "me@example.com"}).fetch()
或
Meteor.users.find({"emails.0": "me@example.com"}).fetch()
【讨论】: