[SOLVED] Need help with server-render and withTracker


#1

I’m building meteor app uses server-render and react-loadable. I constantly get this error, the app needs to restart to make it works again.

E20171228-16:58:47.627(7) (webapp_server.js:798) Error running template: TypeError: Meteor.subscribe is not a function
    at module.exportDefault.withTracker (imports/ui/components/forms/advice-register/adviceRegister.js:176:31)
    at ReactMeteorDataComponent.getMeteorData (packages/react-meteor-data/ReactMeteorData.jsx:181:16)
    at MeteorDataManager.calculateData (packages/react-meteor-data/ReactMeteorData.jsx:34:24)
    at ReactMeteorDataComponent.componentWillMount (packages/react-meteor-data/ReactMeteorData.jsx:130:45)
    at resolve (/home/minhna/WORK/OTV/tvtc/node_modules/react-dom/cjs/react-dom-server.node.development.js:2119:12)
    at ReactDOMServerRenderer.render (/home/minhna/WORK/OTV/tvtc/node_modules/react-dom/cjs/react-dom-server.node.development.js:2260:22)
    at ReactDOMServerRenderer.read (/home/minhna/WORK/OTV/tvtc/node_modules/react-dom/cjs/react-dom-server.node.development.js:2234:19)
    at renderToString (/home/minhna/WORK/OTV/tvtc/node_modules/react-dom/cjs/react-dom-server.node.development.js:2501:25)
    at sink (imports/startup/server/ssr-init.js:18:37)
    at onPageLoad.chain.callback (packages/server-render/server-register.js:13:19)


#2

I think I can fix this error by adding a if condition before:

if (Meteor.subscribe) {
   // Meteor.subscribe goes here
}

But it doesn’t make sense.


#3

Finally, the problem is server-render runs code in withTracker function. It’s on server so there is not Meteor.subscriber function available, we can access any collections because it runs on server.
To solve the problem, I must add some if/else blocks:

  let sub;
  if (Meteor.isClient) {
    sub = Meteor.subscribe('somePublication', { sort: { someField: 1 } });
  }

  if (Meteor.isServer || (sub && sub.ready())) {
    returnObj.someData = SomeCollection.find({}).fetch();
  }
  
  return returnObj;

#4

Hello!. I have the same proplem, but I don’t use react-loadable, and I always get warning in browser
Warning: Did not expect server HTML to contain a <div> in <div>.
I now, that it’s because at first render on client data not loaded yet. But I don’t now how fix this warning?


#5

It only shows on development. When you deploy your app, you won’t see it.