Using the ID passed as a parameter to retrieve data from a collection


#1

Hey guys, so I have a collection posts.

On my /topic page, I create a post and insert it to posts.

You can go to each topic by going to /:category/:postId.

My question is, how do I use postId to retrieve the post in posts?

I’m using FlowRouter and I am able to get the id through params.

FlowRouter.route('/:category/:postId', {
  name: 'viewTopic',
  action: function (params) {
    console.log('You are on topic -', params.postId);

    BlazeLayout.render('viewTopic');
  },
  subscriptions: function (params) {

    this.register('myPost', Meteor.subscribe('posts', params.postId));

  }
});

Now how do I retrieve the data?


#2

Since FlowRouter doesn’t have a way to set the data context in the router like IronRouter does you will need to user a helper on the template.

Template.individualPost.helpers({
    post: function() {
        var postId = FlowRouter.getParam("postId");
        return postsCollection.findOne(postId);
    }
});
<template name="individualPost">
    {{#with post}}

    {{/with}}
</template>

#3

Much love. Thank you for the explanation. I was able to figure it out after posting.