2017-08-07 73 views
1

我喜歡在我的Meteor App上使用服務器渲染包,使用React和React-Router v4。但是,事件的onPageLoad返回一個錯誤:流星服務器渲染路線和Meteor.userId

Error running template: TypeError: Meteor.subscribe is not a function 

這是對createContainer功能:

export default createContainer(() => { 
    const postsHandle = Meteor.subscribe('post'); 
    const loading = !postsHandle.ready(); 
    const postsList = Post.find({ 'draft': false }).fetch(); 
    const listExists = !loading && !!postsList; 
    return { 
     loading, 
     listExists, 
     posts: listExists ? postsList : [], 
    }; 
    }, PostList); 

我不明白我怎麼能得到Meteor.subscribe上的空如果我在服務器上。

任何人有關於我的問題的想法?

謝謝社區!

+0

服務器上沒有訂閱,只發布。您可以直接在服務器上訪問集合,並保證一切都在那裏。 –

回答

0

我面臨同樣的錯誤,這是因爲正如評論中所提到的,服務器上沒有'Meteor.subscribe',所以當代碼通過接收器對象傳遞到服務器上時,會引發錯誤。 即使您的客戶端代碼僅在客戶端上運行,您也應該將您的訂閱包裝在if(Meteor.isClient)中。

也可能出現其他問題,說假設,如果你想閱讀screen.width等類型的屬性,你就必須把它包起來就像上面

所以,你的代碼變得

export default createContainer(() => { 
    var loading,listExists = false 
    var postsList= [] 
    if(Meteor.isClient){ 
     const postsHandle = Meteor.subscribe('post'); 
     loading = !postsHandle.ready(); 
     postsList = Post.find({ 'draft': false }).fetch(); 
     listExists = !loading && !!postsList; 
     } 
     return { 
     loading, 
     listExists, 
     posts: listExists ? postsList : [], 
     }; 
    }, PostList); 

這應該工作正常,只是照顧的變量和功能範圍

+0

這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](https://stackoverflow.com/questions/ask)來提問。您可以[添加賞金](https://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](https://stackoverflow.com/help/)後吸引更多關注此問題什麼聲譽)。 - [來自評論](/ review/low-quality-posts/17204913) –