【问题标题】:Implementing a simple search with Meteor and Iron Router使用 Meteor 和 Iron Router 实现简单的搜索
【发布时间】: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_idcountry_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


    【解决方案1】:

    首先,您似乎期望在您的“探索”路线上有一个 :id 参数。

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

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

    或者在 Router.go 调用中添加一个参数:

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

    其次,您正在尝试使用客户端功能:Session.get() 服务器端。尝试使用参数更新发布;或使用方法调用。

    客户端

    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: server/client,但是 Session.get 的提示在服务器端不可用有帮助!
    猜你喜欢
    • 2017-06-17
    • 2016-03-31
    • 2016-10-31
    • 2023-03-12
    • 2014-05-07
    • 2023-03-30
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多