2013-12-11 33 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的功能。此用戶的頁面。這是否是正確的方法? –