Skip to content
Advertisement

Can not read the slug from url on Flow Router (Meteor)

I’m trying to implement a basic route using Flow Router. But no matter what _id of a collection document I request; I always only get the info about the first document in my collection – ‘Requests’.

So here’s my route definition in the file /lib/routes.js:

FlowRouter.route('/requests/:reqId', {  
  subscriptions: function (params, queryParams) {
    this.register('theRequest', Meteor.subscribe('singleRequest', params.reqId));
  },
  action: function (params, queryParams) {
    FlowLayout.render('layout', { aside: 'aside', main: 'singleRequest' });
    console.log("Yeah! We are on the post:", params.reqId);
  },
  name: 'aRequest'
});

Here’s my helper:

Template.singleRequest.helpers({  
    getRequest: function () {
        return Requests.findOne();
    }
});

Here’s my server publish:

Meteor.publish('singleRequest', function (reqId) {  
    return Requests.find({ _id: reqId});
});

And here’s the template:

<template name="singleRequest">  
  {{#if isSubReady 'theRequest'}}
    {{#with getRequest}}
      <h2>{{_id}}</h2>
      <p>{{reqFrom}}</p>
      <p>{{reqBy}}</p>
    {{/with}}
  {{else}}
    loading...
  {{/if}}
</template> 

What am I doing wrong? Note: In the console, I do see right ‘reqId’ slug due to the console.log command in the route definition. But I do not see the relevant info for the document which it belongs to.

Thanks!

Advertisement

Answer

OK my problem was that I previously had another subscription where I published all the Requests – not just the one with that certain _id. And because I did not create the helper to get only the one with that certain _id; of course server just sent me the very first request.

My solution was only to subscribe to previous subscription and define in the helper to fetch to the request _id:

FlowRouter.route('/requests/:reqId', {  
  subscriptions: function (params, queryParams) {
    this.register('allRequests', Meteor.subscribe('Requests', params.reqId));
  },
  action: function (params, queryParams) {
    FlowLayout.render('layout', { aside: 'aside', main: 'singleRequest' });
    console.log("Yeah! We are on the post:", params.reqId);
  },
  name: 'aRequest'
});

and the helper:

Template.singleRequest.helpers({ 
  getRequest: function () {
    var reqId = FlowRouter.getParam("reqId")
    return Requests.findOne({_id:reqId});
  }    
});
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement