I can’t understand what I’ve done that is incorrect. Perhaps it’s a typo, I’m not sure, but I’m trying server side rendering - which gets the collection fine - obviously.
But when it then switches over to client, the subscription is never ready. Here’s my code:
/imports/api/collections.js
export const Blog = new Mongo.Collection('blog');
...
export const Categories = new Mongo.Collection('categories');
/imports/api/blog/server/publications.js
import { Meteor } from 'meteor/meteor';
import { Blog } from '/imports/api/collections.js';
Meteor.publish('blog', () => {
return Blog.find({
fields: Blog.publicFields
});
});
Then the same thing for categories. Next I render the contents using WithTracker
.
/imports/ui/containers/AppContainer.js
import { Meteor } from 'meteor/meteor';
import React from 'react';
import { withTracker } from 'meteor/react-meteor-data';
import { Blog , Categories } from '/imports/api/collections.js';
import App from '/imports/ui/App.js';
export default AppContainer = withTracker (() => {
const blogHandle = Meteor.isClient ? Meteor.subscribe('blog') : {ready: () => true };
const categoryHandle = Meteor.isClient ? Meteor.subscribe('categories') : {ready: () => true};
const blogs = Blog.findOne();
const categories = Categories.findOne();
console.log(blogHandle.ready());
return {
blogs,
categories,
}
})(App);
I do that ternary operator because you can’t subscribe server side. Also on server side I force ready()
to return true because then I can use the same code for rendering the component based on the subscription returning true.
However when we console.log - as soon as it is client side rendered, we get false
every time. What do I have to do to make the collection ready?