【发布时间】:2015-06-05 10:12:29
【问题描述】:
在我的 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')
令人高兴的是,控制台日志返回所需的 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_id 和 country_b_id) - 这里的目的是允许用户输入两个国家,对应的 @ 987654330@然后正在返回。
我目前正在努力将所有部分联系在一起 - 搜索时的控制台输出当前是:
Uncaught Error: Missing required parameters on path "/explore/:_id". The missing params are: ["_id"]. The params object passed in was: undefined.
任何帮助将不胜感激 - 因为您可能会说我是 Meteor 的新手,并且仍在习惯 pub/sub 方法!
已编辑:当我第一次发布时,将发布方法的客户端/服务器混为一谈 - 深夜发布的危险!
【问题讨论】:
标签: meteor coffeescript publish-subscribe iron-router