1

This method is called by a helper attached to a post. For some reason, even though the user is definitely in the collection, I get TypeError: Cannot read property 'profile' of undefined from the method when it gets called. What's the deal?

userImage: function(user) {
    var userObject = Meteor.users.findOne({ "username": user }, { profile: { image: 1 } });
    return userObject.profile.image;
}

Peripheral question, can I just call a method in a helper like this and have it return right through to the helper in the template?

userImage: function() {
    var user = this.username;

    Meteor.call('userImage', user, function(error,id) {
        if (error) {
            return console.log(error.reason);
        }
    });
}
Jack Reid
  • 87
  • 2
  • 9
  • possible duplicate of [Meteor findOne query returns undefined in one template helper. In other template helpers, same query works well](http://stackoverflow.com/questions/17728386/meteor-findone-query-returns-undefined-in-one-template-helper-in-other-template) – Ryne Everett Mar 26 '15 at 00:41

1 Answers1

2

I think you mean:

Meteor.users.findOne({username: user}, {fields: {'profile.image': 1}});

You should probably add a guard after that like:

if(userObject && userObject.profile)
  return userObject.profile.image;

See this question for how to call a method from your helper.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • I sorted out that Mongo syntax but userObject stays undefined. do you have any other ideas? Perhaps I'm going about this from the wrong angle. I'm really just trying to write a helper that will retrieve the user's profile.image to display in a post, but it seems Meteor.users won't let me access profile unless the user is logged in. – Jack Reid Feb 21 '15 at 18:11
  • 1
    Just to clarify one point, when you say "the user is definitely in the collection" do you mean also that the user is definitely published to your client side collection? – Jeremy S. Feb 21 '15 at 18:49
  • Okay, so I've been way overcomplicating this. I've published the profile data and subscribed to it on the client. Now the whole lookup fits into a three-line helper: `var userObject = Meteor.users.findOne({username: this.username});` then `return userObject.profile.image;`. – Jack Reid Feb 21 '15 at 19:56
  • Yes, that looks correct. Please make sure not to publish too much data about your users - see the "published secrets" section [here](http://dweldon.silvrback.com/common-mistakes). – David Weldon Feb 22 '15 at 02:16