2014-10-30 62 views
1

我試圖運行Meteor.methods調用這裏面的功能:如何在服務器上單獨運行一個函數(避免minimongo)?

geoSearch = function(long, lat, m) { 
    return Sparks.find({ 'geo.coordinates' : 
     { $geoWithin : 
     { $centerSphere : 
      [ [ long, lat ] , m/3959 ] 
    } } }); 
} 

Minimongo不承認$geoWithin。好的,那麼如何在服務器上單獨運行此查詢(避免客戶端)?我試着將這個功能移動到/server並從Meteor.methods調用中調用它,但geoSearch然後是undefined。好的,現在,我如何從客戶端調用geoSearch

我相信我的問題有一個簡單的答案,我只是沒有想到它。謝謝。

回答

1

在服務器上定義的方法在server目錄下的文件:

Meteor.methods({ 
    geoSearch: function (long, lat, m) { 
    check(long, Number); 
    check(lat, Number); 
    check(m, Number); 

    return Sparks.find... 
    }, 

}); 

然後調用它的客戶端上:

Meteor.call('geoSearch', long, lat, m , function (error,result) { 
    ... 
}); 
+0

多虧了你們倆。我不知道爲什麼我忘記使用Meteor.call。我知道我做錯了什麼,哈哈。再次感謝。 – Nathan 2014-10-30 01:10:57

1

可以從Meteor.method範圍內使用isSimulation屬性:

https://docs.meteor.com/#/full/method_issimulation

Meteor.methods({ 
    geoSearch: function(long, lat, m) { 
    if(this.isSimulation){ 
     return; 
     } 
    return Sparks.find({ 'geo.coordinates' : 
     { $geoWithin : 
     { $centerSphere : 
     [ [ long, lat ] , m/3959 ] 
    } } }); 
    } 
}); 

然後從客戶端只需用一組正確的參數和回調Meteor.call("geoSearch",...,function(error,result){...});獲取的結果,它會只能在服務器上調用$geoWithin的東西,並且在客戶端上不做任何事情。

相關問題