使用流星和铁路路由器实现一个简单的搜索
问题描述:
在我的流星旅程的下一个阶段(阅读:学习绳索!),我想实现一个基于用户输入值的简单搜索,然后重定向到一个特定的路由到从服务器返回的记录。使用流星和铁路路由器实现一个简单的搜索
目前,我通过这个代码拿起在输入的值:
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
最后,我有一个search.coffee文件在我/ lib目录下定义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
) - 这里的目的是让用户输入两个国家,然后返回相应的CountryPair
。
我目前正在努力所有的作品联系在一起 - 在搜索控制台输出是目前:
Uncaught Error: Missing required parameters on path "/explore/:_id". The missing params are: ["_id"]. The params object passed in was: undefined.
任何帮助将不胜感激 - 因为你可能会说我是新来的流星我仍然习惯于发布/订阅方法!
编辑:混淆客户端/服务器的发布方法,当我第一次发布 - 深夜发布的危险!
答
第一个,你似乎期待在'探索'路线上有一个:id参数。
如果我理解你的情况下,你不希望这里的任何参数,可以使您可以直接删除:从您的路线“ID”:
@route "explore",
path: "/explore/"
waitOn: ->
Meteor.subscribe 'countryPairsSearch'
或任何添加PARAMS到路由器。请拨打:
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 }
]
谢谢 - 我居然做了一个错字RE:在服务器/客户端我原来的问题,但在会议上的头。得不到服务器端的帮助! – Budgie 2015-04-01 07:57:27