1
votes

I yould like use server-render package on my Meteor App, using React and React-Router v4. But, onPageLoad event return an error :

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

It's on the createContainer function :

  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);

I don't understand how i can get Meteor.subscribe on null if i'm on the server.

Anyone have idea about my problem ?

Thank you community !

1
There is no subscribe on the server, only publish. You can access collections directly on the server and be guaranteed everything is there. - Michel Floyd

1 Answers

0
votes

I faced the same error, it is because as mentioned in the comment, there is no 'Meteor.subscribe' on the server, so when the code gets passed onto the server via the sink object, it raises an error. You should wrap your subscriptions in a if(Meteor.isClient), even if your client code is running on ONLY the client.

Other issues can also occur, say suppose if you are trying to read the screen.width, etc type of properties, you would have to wrap it just like above

So your code becomes

  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);

This should work fine, just take care of the variables and the functional scopes