Search code examples
meteorviewflow-routermongo-collection

Meteor: single post view, via id and flow router is not parsing any data from the collection


Im trying to open a single record from my #each loop of items into its own view by clicking a link that says 'see more', which will take me to the single article. I set up my Flow router and its working but i cannot see the data that's supposed to come in.

the template helper for the single article looks like this

Template.collectionSingle.helpers({
    articles: function () {
      var id = FlowRouter.getParam('_id')
      return theCollection.findOne({_id: id});
    }
  });

}

my route looks like this

FlowRouter.route('/collection/:_id', {
    name: 'collection',
    action() {
        BlazeLayout.render("AppLayout", {main: "collectionSingle"});
    }
});

and the template "collectionSingle" looks like this

<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
  <img src="{{thumbnail}}" alt="" />
</template>

when i navigate to http://localhost:3000/collection/thePublication-0 all i see is the test message 'This is a test', but i don't see the {{title}} nor the thumbnail.

furthermore, when i change:

return theCollection.findOne({_id: id});

to another one of my collections :

return OtherCollection.findOne({_id: id});

http://localhost:3000/collection/thePublication-0 

remains the same.

how can i successfully have a single articles page for each of my articles and have them linked properly with flow router?


Solution

  • You need to actually use your template helper that is returning the data context:

    <template name="collectionSingle">
    <h1>{{title}}</h1>
    <h1>This is a test</h1>
    {{#with articles}}
      <img src="{{thumbnail}}" alt="" />
    {{/with}}
    </template>
    

    Since your articles helper returns a single document you use {{#with articles}}. If it returned a cursor or array you would loop over that with {{#each articles}}. The normal convention is to use the singular form for the former, the plural for the latter.