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.

1
  • 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. :) Commented Mar 2, 2016 at 23:09

2 Answers 2

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.

1
  • 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 :) Commented Mar 2, 2016 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.