dado_eyad
dado_eyad

Reputation: 601

Meteor Querying other users by email

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?

Upvotes: 5

Views: 6614

Answers (3)

Ola Wiberg
Ola Wiberg

Reputation: 1679

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.

Upvotes: 12

Jagdish Barabari
Jagdish Barabari

Reputation: 2703

First you need to publish the users as mentioned above answer and run the following command

Meteor.users.find({"emails": "[email protected]"}).fetch()

OR

Meteor.users.find({"emails.0": "[email protected]"}).fetch()

Upvotes: 1

sohel khalifa
sohel khalifa

Reputation: 5578

This may be helpful:

 var text = "[email protected]";
 Meteor.users.findOne({'emails.address': {$regex:text,$options:'i'}});

Also see Advance Queries

Upvotes: 4

Related Questions