flowen
flowen

Reputation: 536

meteor - get value from collection serverside

I want to send an email from the current_user(client) to userY(a member) without exposing the emailaddress to the client. So all serverside.

I have the _id of userY (from the router param: email.toUser = Router.current().params._id;) and send it as a value to a method.

In the method function I want to do something like

var to = Meteor.users.find({ _id: email.toUser }); 

Now when I console.log(to) I get a huge _mongo object instead of the user profile (I expected to be able to log: to.profile.email) what's the best way to get the value from the email field?

Upvotes: 2

Views: 1398

Answers (2)

Salketer
Salketer

Reputation: 15711

What you are seeing is the cursor to the collection set.

To retrieve the records, you can use .fetch()

var to = Meteor.users.find({ _id: email.toUser }).fetch();
console.log(to[0].profile.email);

As you are looking up by ID, you are expecting only 1 result, so you could also use findOne() instead of find() this will return the first element directly.

var to = Meteor.users.findOne({ _id: email.toUser });
console.log(to.profile.email);

EDIT: I'd like to add that replacing { _id: email.toUser } by email.toUser should work. When using just the ID, there is no need to pass an object.

Upvotes: 5

Ethaan
Ethaan

Reputation: 11376

You should change find to findOne

Like this.

var to = Meteor.users.findOne({ _id: email.toUser }); 

find return the whole Mongo.Collection cursor

Upvotes: 3

Related Questions