Meteor Mongo findOne在方法中返回undefined

时间:2015-02-21 17:40:43

标签: mongodb meteor

此方法由附加到帖子的助手调用。出于某种原因,即使用户肯定在集合中,我也会在调用时从方法中获取TypeError: Cannot read property 'profile' of undefined。这是什么交易?

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

外围问题,我可以在这样的帮助器中调用一个方法,让它直接返回到模板中的帮助器吗?

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

    Meteor.call('userImage', user, function(error,id) {
        if (error) {
            return console.log(error.reason);
        }
    });
}

1 个答案:

答案 0 :(得分:2)

我认为你的意思是:

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

之后你应该添加一个guard,如:

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

有关如何从助手调用方法的信息,请参阅this question

相关问题