Search code examples
javascriptnode.jsmeteoriron-router

Retrieving a display results from a search


I'm relatively new to meteor.js and I'm trying to get a search form to work. So far I'm not even trying to get the params to work, but it will come later.

I'm basically trying to get a bunch of lifts to display.

lib/router.js

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading',
  notFoundTemplate: 'notFound',
  waitOn: function() {
    return Meteor.subscribe('lifts');
  }
});

Router.route('/', { name: 'liftsList' });

Router.route('/lifts/search/:from-:to-:when', {
  name: 'liftsSearch',
  waitOn: function() {
    return Meteor.subscribe('liftsSearch');
  }
});

server/publications.js

Meteor.publish('liftsSearch', function() {
  var query = { fromLoc: { $near : { 
    $geometry: { 
      type : "Point" ,
      coordinates: [ 6.11667, 45.9 ]
    } },
    $maxDistance : 50
  }};

  return Lifts.find(query);
});

If I try to display the results with Lifts.find(query).fetch(), it returns actual results.

client/lifts_search.html

<template name="liftsSearch">
  <div class="container">
    <h3>Lifts search results {{hi}}</h3>
    <div class="lifts">
      {{#each lifts}}
        hi
        {{> liftItem}}
      {{/each}}
    </div>
  </div>
</template>

Here I simply got no lifts displaying, not even the little "hi" string.

Thanks


Solution

  • Unless there's code that you haven't included, {{#each lifts}} isn't rendering because you're not defining lifts anywhere. Just because you're populating the Lifts collection, the template doesn't automatically known that lifts refers to it (largely because that would be totally arbitrary - what exact query would it refer to?).

    So, you need to define lifts in either a router data function:

    Router.route('/lifts/search/:from-:to-:when', {
      name: 'liftsSearch',
      waitOn: function() {
        return Meteor.subscribe('liftsSearch');
      },
      data: function() {
        return {
          lifts: Lifts.find() // add a query if you want
        }
      }
    });
    

    Or in a template helper:

    Template.liftsSearch.helpers({
      lifts: function() {
        return Lifts.find(); // add a query if you want
      }
    });