Search code examples
meteorcoffeescriptpublish-subscribeiron-router

Implementing a simple search with Meteor and Iron Router


In the next phase of my Meteor journey (read: learning the ropes!), I'd like to implement a simple search based on user inputed values, then redirect to a route specific to the record returned from the server.

At the moment, I'm picking up the inputed values via this code:

Template.home.events 'submit form': (event, template) ->
  event.preventDefault()
  console.log 'form submitted!'
  countryFirst = event.target.firstCountrySearch.value
  countrySecond = event.target.secondCountrySearch.value
  Session.set 'countryPairSearchInputs', [countryFirst, countrySecond]
  countryPairSearchInputs = Session.get 'countryPairSearchInputs'
  console.log(countryPairSearchInputs)
  return Router.go('explore')

Happily, the console log returns the desired countryPairSearchInputs variable - an array of two ids. In my routes.coffee file I then have the following:

@route "explore",
    path: "/explore/:_id"
    waitOn: ->
      Meteor.subscribe 'countryPairsSearch'

On the server side, I have:

Meteor.publish 'countryPairsSearch', getCountryPairsSearch

And finally, I have a search.coffee file in my /lib directory that defines the getCountryPairsSearch function:

@getCountryPairsSearch = ->
  CountryPairs.findOne $and: [
    { country_a_id: $in: Session.get('countryPairSearchInputs') }
    { country_b_id: $in: Session.get('countryPairSearchInputs') }
  ]

With regards to the search function itself, I have a CountryPairs collection where each record has two ids (country_a_id and country_b_id) - the aim here is to allow users to input two countries, with the corresponding CountryPair then being returning.

I'm currently struggling to tie all the pieces together - the console output on searching is currently:

Uncaught Error: Missing required parameters on path "/explore/:_id". The missing params are: ["_id"]. The params object passed in was: undefined.

Any help would be greatly appreciated - as you can probably tell I'm new to Meteor and am still getting used to the pub/sub methodology!

Edited: mixed up client/server for the publish method when I first posted - the danger of late-night posting!


Solution

  • First, seems you're expecting an :id parameter on your 'explore' route.

    If I understand you're case, you're not expecting any params here, so you can just delete ':id' from your route:

    @route "explore",
    path: "/explore/"
    waitOn: ->
      Meteor.subscribe 'countryPairsSearch'
    

    or either add a params to your Router.go call:

    Router.go('explore', {_id: yourIdVar});
    

    Secondly, you're trying to use a client function: Session.get() server-side. Try to update the publication using a parameter ; or using a method.call.

    client-side

    Meteor.subscribe 'countryPairsSearch' countryA countryB
    

    not sure about the coffeescript syntax, check http://docs.meteor.com/#/full/meteor_subscribe

    and server-side

    @getCountryPairsSearch = (countryA, countryB) ->
      CountryPairs.findOne $and: [
        { country_a_id: $in: countryA }
        { country_b_id: $in: countryB }
      ]