0

I have a route defined in my Meteor + React app like this below. I would like to fetch the post and pass into component via props. But I get an error saying post is undefined when I reload the page when on that URL. I can visit the URL when I go from other parts from the site but not when reloading.

const Posts = new Mongo.Collection('posts');

FlowRouter.route('/post/:slug', {
  name: 'Post.show',
  action(params) {
    let post = Posts.findOne({slug: params.slug});
    mount(Layout, {
      main: () => <Post key={post._id} post={post} />,
    });
  },
});

Perhaps this isn't the way to go but I would like to have the ability to pass in custom posts thats why I'm using this approach. Does it have anything to do with that the Posts collection isn't fully initiated? If so how do I wait for it to be initiated?

trevligheten
  • 306
  • 2
  • 13

1 Answers1

0

Posts might be empty when you reload the page and the route action is executed. Instead of this, you should create a reactive component using createContainer.

See https://www.meteor.com/tutorials/react/collections

aedm
  • 5,596
  • 2
  • 32
  • 36