Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor Querying other users by email

Tags:

mongodb

meteor

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?

like image 429
dado_eyad Avatar asked Nov 26 '12 11:11

dado_eyad


2 Answers

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.

like image 145
Ola Wiberg Avatar answered Sep 22 '22 13:09

Ola Wiberg


This may be helpful:

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

Also see Advance Queries

like image 22
sohel khalifa Avatar answered Sep 22 '22 13:09

sohel khalifa