1

I have a simple function that goes like this:

getCurrentUserInfo: function (userId, reference) {
    var scanUser = Meteor.users.find({"_id": userId}).fetch()[0];
    return scanUser.reference;
  }

When user the function like this:

getCurrentUserInfo(Meteor.userId(), "_id");

It gives me "undefined" result which I believe it is due to the string parameter "_id" because it works if I return scanUser._id directly . I am not sure how to make it work. Any suggestion or related topic about it? Thank you.

Website Is Fun
  • 290
  • 6
  • 20
  • Stephen Woods' answer below did the trick, I thought adding `[0]` at the end of my query would remove the array property of scanUser but it did not so I have to use `scanUser[reference]` and that sorted it out. :) – Website Is Fun Mar 02 '16 at 23:09

2 Answers2

1

Try this:

getCurrentUserInfo: function (userId, reference) {
    var scanUser = Meteor.users.find({"_id": userId}).fetch()[0];
    return scanUser[reference];
}

You need to access properties of objects by string using bracket notation.

Stephen Woods
  • 4,049
  • 1
  • 16
  • 27
  • Stephen Woods - OMG that is quick, now I realize that the scanUser is an array so I should use array. I thought by adding [0] at the last of the query would remove the array. Thanks a lot Mr. Woods :) – Website Is Fun Mar 02 '16 at 23:07
1

return scanUser._id; since you implicitily wrote _id inside the function

return scanUser[reference] which is more generic and works for any property

Qaddura
  • 177
  • 6