使用 Meteor 和 Iron Router 实现简单搜索

Implementing a simple search with Meteor and Iron Router

在我的 Meteor 旅程的下一阶段(阅读:学习技巧!),我想根据用户输入的值实现一个简单的搜索,然后重定向到特定于从服务器返回的记录的路由.

目前,我正在通过以下代码获取输入的值:

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')

令人高兴的是,控制台记录 returns 所需的 countryPairSearchInputs 变量 - 两个 ID 的数组。在我的 routes.coffee 文件中,我有以下内容:

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

在服务器端,我有:

Meteor.publish 'countryPairsSearch', getCountryPairsSearch

最后,我的 /lib 目录中有一个 search.coffee 文件,它定义了 getCountryPairsSearch 函数:

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

关于搜索功能本身,我有一个 CountryPairs 集合,其中每条记录都有两个 ID(country_a_idcountry_b_id)- 目的是让用户能够输入两个国家,对应的CountryPair返回

我目前正在努力将所有部分联系在一起 - 搜索的控制台输出目前是:

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

任何帮助将不胜感激 - 你可能会说我是 Meteor 的新手,并且仍在习惯 pub/sub 方法!

已编辑:我第一次发帖时混淆了发布方法 client/server - 深夜发帖的危险!

First,看来您希望 'explore' 路线上有一个 :id 参数。

如果我理解你的情况,你不需要这里有任何参数,所以你可以从你的路线中删除':id':

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

或者在您的 Router.go 调用中添加参数:

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

其次,您正在尝试使用客户端函数:Session.get() 服务器端。尝试使用参数更新发布;或使用 method.call。

客户端

Meteor.subscribe 'countryPairsSearch' countryA countryB

不确定 coffeescript 语法,检查 http://docs.meteor.com/#/full/meteor_subscribe

和服务器端

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