2013-12-11 69 views
0

我想从服务器获取文档并将其显示在客户端上,但订阅始终返回没有文档的集合。流星:订阅不起作用

// server/publications.js 
Meteor.publish('myPages', function() { 
    return Pages.findOne({userId: this.userId}); 
}); 

// collection/pages.js 
MyPages = new Meteor.Collection('myPages'); 

// client/main.js 
Meteor.subscribe('myPages'); 

// client/view.js 
Template.myView.helpers({ 
    myPages: function(e, t) { 
     console.debug(MyPages.find({})); 
     return MyPages.find({}); 
    } 
}); 
+0

用户是否登录?如果用户未登录,则this.userId将为null,这可能不是您想要的。另外,在'Template.myView.helpers'中有意使用'Meteor.publish'中的'Pages'与'MyPages'? “MyPages”的定义在哪里? –

+0

我的用户已登录。是的,这是故意的,因为我希望客户端只能访问他的页面。这是否是正确的方法? –

回答

1

无法通过订阅移动集合之间的文件。如果您订阅获取Pages收藏中的文档(定义为new Meteor.Collection("pages")),则无论您的pubsub频道如何,客户端上的文档将在定义为new Meteor.Collection("pages")的集合中找到。因此,删除MyPages的所有痕迹,并在客户端上使用Pages。你会在那里找到文件。

1

我不认为你可以使用findOne发布集合:它不会返回游标而是实际的对象。

这不行吗?

Meteor.publish('myPages', function() { 
    return Pages.find({userId: this.userId}); 
}); 

或者,如果必要的话:

Meteor.publish('myPages', function() { 
    return Pages.find({userId: this.userId}, {limit: 1}); 
}); 
+0

它仍然返回一个空集合。而且我知道Pages集合不是空的,因为它从控制台(Pages.find({}))返回文档并且启用了autopublish。 –

+0

网页或MyPages?你似乎在混合两者。什么是页面? –

+0

页面是每个页面(来自每个用户)的整个集合。在这个集合中,我试图返回登录用户的页面(他不需要获取其他用户的页面)。这就是MyPages的功能。此用户的页面。这是否是正确的方法? –