Flow Router - can't get the slug to retrieve relevant data


#1

Hello dear Meteoristas!

I have asked the same question on Stackoverflow as well but thought good idea to ask here too. Cause I didn’t get an answer yet.

Any help would be very appreciated…

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. I’m guessing the reason is that the slug - reqId doesn’t reach the server??

Thanks!


#2

You should be using either Request.findOne() and {{#with}} or Request.find() and {{#each}}. I haven’t tried this recently but I remember running into the same issue and I have kept to this pattern since then. findOne return an object but find returns a cursor (even if you only have 1 object in there) and this is throwing off {{#with}}


#3

What happens when you check in your console if the requested (subscribed) document is there?


#4

Nothing. It only shows the relevant and actually required document that I’d like to see on the page. Because I have this on the route definition:

console.log("Yeah! We are on the post:", params.reqId);

The problem, therefore is on the server side not being able to fetch the right document I’m guessing…


#5

Thanks Valentin. I have tried it already before but it doesn’t work that way either… - with Requests.fineOne() It doesn’t return anything… :confused:


#6

So Requests.find().fetch() in the browser console shows only one document? What if you connect to the shell and run the command in the subscription on the server?

Like

meteor shell
> Requests.find({_id: <one of your IDs>}).fetch()

#7

Thanks jamgold! I think, with the help of you as well, I found the problem. The thing is that I have another subscription that I have which returns all the requests to the user that is logged in. And because user is already subscribed to that and in the helper it’s not defined which _id; of course the server returns the 1st document _id that user subscribed from the other subscription.

Hmmm, now I think I can form the logic better and subscribe to previous subscription and it should be fine!

Thanks guys! :smile: