2015-03-31 112 views
2

在我的流星旅程的下一個階段(閱讀:學習繩索!),我想實現一個基於用戶輸入值的簡單搜索,然後重定向到一個特定的路由到從服務器返回的記錄。使用流星和鐵路路由器實現一個簡單的搜索

目前,我通過這個代碼拿起在輸入的值:

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_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. 

任何幫助將不勝感激 - 因爲你可能會說我是新來的流星我仍然習慣於發佈/訂閱方法!

編輯:混淆客戶端/服務器的發佈方法,當我第一次發佈 - 深夜發佈的危險!

回答

1

第一個,你似乎期待在'探索'路線上有一個: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 } 
    ] 
+0

謝謝 - 我居然做了一個錯字RE:在服務器/客戶端我原來的問題,但在會議上的頭。得不到服務器端的幫助! – Budgie 2015-04-01 07:57:27

相關問題