Meteor Mongo findOne returns undefined in method

此生再无相见时 提交于 2020-01-06 01:45:41

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/28648980/meteor-mongo-findone-returns-undefined-in-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!