Jack Reid
Jack Reid

Reputation: 87

Meteor Mongo findOne returns undefined in method

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);
        }
    });
}

Upvotes: 1

Views: 5468

Answers (1)

David Weldon
David Weldon

Reputation: 64342

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.

Upvotes: 2

Related Questions