Get _id of any user (onServer)

Hi,

When I run this user = Meteor.users.find({username: 'test'}).fetch();
The user is assign with the JSON of that user.

But how do I access the properties? when I try console.log(user._id) it just returns “undefined”…

tnx!

Because you are doing fetch, user is an array of objects. try user[0]._id.
it is also better to use fineOne instead of find().fetch(), which basically means same thing, but more obvious that you are expecting one record only.

1 Like

If you are positive there’s only one user with that username, use findOne instead of find. Then you can also skip using fetch.

var userID = Meteor.users.findOne({username: 'test'})._id;

tnx @greblak, @oceansgem,

I used findOne and it works :smiley:

Just remember that findOne only returns one result regardless of if there’s more. It just returns the first. It’s kinda like doing Collection.find().fetch()[0]
It’s possible to do Meteor.users.findOne() with no arguments and get the first user in the collection. So make sure you only search for unique documents :slight_smile: