使用Meteor和Iron路由器实现简单搜索
在Meteor旅程的下一个阶段(阅读:学习诀窍!),我想基于用户输入的值实现一个简单的搜索,然后重定向到特定于服务器返回的记录的路由 目前,我正在通过以下代码获取输入的值:使用Meteor和Iron路由器实现简单搜索,meteor,coffeescript,publish-subscribe,iron-router,Meteor,Coffeescript,Publish Subscribe,Iron Router,在Meteor旅程的下一个阶段(阅读:学习诀窍!),我想基于用户输入的值实现一个简单的搜索,然后重定向到特定于服务器返回的记录的路由 目前,我正在通过以下代码获取输入的值: Template.home.events 'submit form': (event, template) -> event.preventDefault() console.log 'form submitted!' countryFirst = event.target.firstCountrySear
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\u a\u id
和country\u b\u id
)-这里的目的是允许用户输入两个国家,然后返回相应的CountryPairs
我目前正在努力将所有的部分结合在一起-搜索时的控制台输出当前为:
Uncaught Error: Missing required parameters on path "/explore/:_id". The missing params are: ["_id"]. The params object passed in was: undefined.
任何帮助都将不胜感激——正如你可能知道的那样,我对Meteor是新手,并且仍然习惯于发布/订阅方法
编辑:当我第一次发布时,发布方法的客户端/服务器混合-深夜发布的危险 首先,似乎您希望“探索”路线上有一个:id参数 如果我理解您的情况,您不希望这里有任何参数,因此您可以从路由中删除“:id”:
@route "explore",
path: "/explore/"
waitOn: ->
Meteor.subscribe 'countryPairsSearch'
或者将参数添加到路由器。转到呼叫:
Router.go('explore', {_id: yourIdVar});
其次,您正在尝试使用一个客户端函数:Session.get()服务器端。尝试使用参数更新发布;或者使用method.call
客户端
Meteor.subscribe 'countryPairsSearch' countryA countryB
不确定coffeescript语法,请检查
和服务器端
@getCountryPairsSearch = (countryA, countryB) ->
CountryPairs.findOne $and: [
{ country_a_id: $in: countryA }
{ country_b_id: $in: countryB }
]
谢谢-我在最初的问题中确实输入了一个错误:server/client,但是Session.get不可用服务器端帮助!