mongodbmeteoriron-routermeteor-blaze

after using $and query, the results aren't render on DOM


I am building some little log reporting with meteor.

have a dead simple table, that should contain all the data that received from the external mongodb server.

the html:

 <tbody>
  {{#each impressions}}
  <tr>
    {{> impression}}
  </tr>
  {{/each}}
</tbody>

js :

Meteor.subscribe('impressions');
...
...
Template.logResults.helpers({
'impressions': function() {
var sTs = Router.current().params.fromTs;
var eTs = Router.current().params.toTs;
return Impressions.find({});
}
});

So far, so good. BUT, when I am changing the query to this one :

Impressions.find({
  $and: [{
    ts: {
      $gte: sTs
    }
  }, {
    ts: {
      $lte: eTs
    }
  }]
});

The results aren't displayed on the HTML DOM, I tried to debug that, and created a console.log of this exact query, and surprisingly all the correct results return successfully to the console.

screenshot attached. I am probably doing something wrong, maybe with publish/subscribe thing.

help someone?

Thanks. P.S. I removed the insecure and auto-publish, have this code on the server folder

Meteor.publish('impressions', function() {
  return Impressions.find();
});

and this code on the main lib folder

Impressions = new Mongo.Collection("banners");

enter image description here


Solution

  • The router stores the parameters for the current route as strings (which makes sense because URLs are strings), so you need to explicitly convert the values to integers before querying the database. Give something like this a try:

    var sTs = Number(Router.current().params.fromTs);
    var eTs = Number(Router.current().params.toTs);
    

    Notes:

    1. parseInt or parseFloat may be a better choice depending on the nature of your input. See this question for more details.
    2. You could do the type conversion in the router itself and pass the values in the data context to the helpers. See this section of the iron router guide.
    3. I suspect it worked when you typed it into the console because you used numbers instead of strings. E.g. ts: {$gte: 123} instead of ts: {$gte: '123'}.