2016-08-14 67 views
0

我有一个meteor.js应用程序,我试图生产。我有一个集合UserEarnings,其中存储了每个用户已获得的“积分”记录。现在,我在发布的服务器上的所有记录:为什么当我在服务器上进行过滤时,Meteor.js发布不会返回任何结果?

Meteor.publish('userEarnings',() => UserEarnings.find()); 

和过滤记录下来,只在相关的多个客户端上的特定用户:

const composer = (props, onData) => { 
    const subscription = Meteor.subscribe('userEarnings'); 
    if (subscription.ready()) { 
     const userEarnings = UserEarnings.find({owner: Meteor.user()._id}).fetch(); 
     onData(null, { userEarnings }); 
    } 
}; 

export default composeWithTracker(composer, Loading)(AuthenticatedNavigation); 

这是工作得很好,但最好我会被过滤在服务器上:

Meteor.publish('userEarnings',() => UserEarnings.find({ owner: this.userId })); 

不幸的是,这样做产生了未返回任何结果当我打电话UserEarnings.find()获取()在客户端上。有谁知道这可能是为什么?请注意,我仅在订阅用户时订阅来自客户端的'userEarnings'。

我不确定是否有任何其他信息是相关的,但请让我知道,如果你认为有。我跑流星1.4

回答

2

变化:

Meteor.publish('userEarnings',() => UserEarnings.find({ owner: this.userId })); 

到:

Meteor.publish('userEarnings', function userEarnings() { 
    return UserEarnings.find({ owner: this.userId }); 
}); 

为什么第一种方法是不工作的原因是因为通过你设置一个箭头功能发布函数的上下文是回调。 Meteor.publish需要非箭头功能才能正确设置上下文(并允许您正确使用this.userId)。

+0

非常感谢!我正试图弄清楚这件事。 – Swiss

相关问题