I'm trying to query users by emails with the following command
Meteor.users.findOne({'emails.address': '[email protected]'});
It works in the mongo shell but it returns undefined in Meteor.
Any ideas?
UPDATE
Turned out that I'm not able to query other users. The same query works when I query the logged in user email. So the question now how can I query on all the users?
By default, Meteor only publishes the logged in user and you can, as you mention, run queries against that user. In order to access the other users you have to publish them on the server:
Meteor.publish("allUsers", function () {
return Meteor.users.find({});
});
And subscribe to them on the client:
Meteor.subscribe('allUsers');
Also keep in mind that you might not want to publish all the fields so you can specify what fields you like to publish/not publish:
return Meteor.users.find({},
{
// specific fields to return
'profile.email': 1,
'profile.name': 1,
'profile.createdAt': 1
});
Once you have published the collection, you can run queries and access information for all the users.
This may be helpful:
var text = "[email protected]";
Meteor.users.findOne({'emails.address': {$regex:text,$options:'i'}});
Also see Advance Queries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With