How to get Meteor.users data in server/methods.js



I was really having a hard time fetching the _id of a particular user when done in server-side via methods.

   getUserIdByEmail: function (email) {
      var user = Meteor.users.find({'emails.0.address': email}, {_id: 1}).fetch();

     return user;

When I do console.log(user), it’s returning

 I20150331-16:50:53.037(8)? { _id: '9bAmuYkLdxPqxxxx',
 I20150331-16:50:53.037(8)?   createdAt: Sun Mar 29 2015 10:10:44 GMT+0800 (PHT),
 I20150331-16:50:53.037(8)?   services:
 I20150331-16:50:53.037(8)?    { password: { bcrypt: '$xxxxxxxxvUIew.gJ8Xxxxx8Xxx8Xxx8Xxx8Xxx8Xxxxx' },
 I20150331-16:50:53.037(8)?      resume: { loginTokens: [] } },
 I20150331-16:50:53.038(8)?   emails: [ { address: '', verified: true } ],
 I20150331-16:50:53.038(8)?   profile:
 I20150331-16:50:53.038(8)?    { first_name: 'Arvi',
 I20150331-16:50:53.038(8)?      middle_initial: 'W',
 I20150331-16:50:53.038(8)?      last_name: 'D'} }

How do I get the id? I tried user._id, user.profile… but it didn’t work.



That’s because you get an array of users from .find().fetch(). Use .find().fetch()[0] or just findOne(), which is nicer. And it’s reactive, whereas the other solution isn’t in some cases.


@fvg I see! Thank you. :slight_smile: I can now get the user id.


Both are equally reactive.