[Solved] Getting value for a specific user

Hello,
I’m trying to display how much is user being paid using Mongo query.
So far it claims that payment is undefenied, yet I see it if I type Meteor.users.find().fetch().

My query

payment = Meteor.users.findOne({"_id" : {$eq: Meteor.userId()}}).payment;

This is what I get upon Meteor.users.find().fetch()

Meteor.users.find().fetch()
[Object]0: Objectemails: 
Array(1)
profile: Object
   name_first: "Igor"
   name_last: "Line"
   payment: "10.5"
   username: "il"
__proto__: Objectroles: Array(1)
   username: "il"
_id: "MWu4kS5fpNCLuo9aS"
__proto__: Objectlength: 1__proto__: Array(0)

I do get a current number upon using this:

Meteor.userId().users.find().fetch()[0].profile.payment

but I need to use findOne with _id filter.

Try to this one: Meteor.user().profile.payment

Why you need _id of current user??

1 Like

I tried this and it works.

var payment = Meteor.users.findOne({"_id" : {$eq: Meteor.userId()}}).profile.payment;

Why i need _id.

I have Meteor.users() published fully for admins, and users can only see their own data.
So while for users I can use Meteor.userId().users.find().fetch()[0].profile.payment to find their own hourly payment, admins will see the very first user’s payment instead of their own.
So I need to perform a query filtering by user’s ID.

I don’t understand…

Meteor.users.findOne({"_id" : {$eq: Meteor.userId()}}) it is the same as Meteor.user(). In this case registered users can only see their own data.

Admins can see own data and all users too, because publication. If admin needs to find specific user: Meteor.users.findOne({ "_id": user._id })

Or I can’t understand the question…

So you answered you question, right? You missed profile in original code.

2 Likes