Nathan
Nathan

Reputation: 470

How to run a function on the server alone (avoiding minimongo)?

I'm trying to run this function inside of a Meteor.methods call:

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

Minimongo does not recognize $geoWithin. Okay, so how do I run this query on the server alone (avoiding the client)? I tried moving this function to /server and invoking it from within the Meteor.methods call, but geoSearch was then undefined. Okay, so now, how do I invoke geoSearch from the client?

I'm sure there is an easy answer to my problem, I'm just not thinking of it. Thanks.

Upvotes: 1

Views: 108

Answers (2)

Dan Dascalescu
Dan Dascalescu

Reputation: 152095

Define the method on the server in a file under the server directory:

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

    return Sparks.find...
  },

});

Then call it on the client:

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

Upvotes: 1

saimeunt
saimeunt

Reputation: 22696

You can use the isSimulation property from within a Meteor.method context :

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 ]
    } } } );
  }
});

Then from the client just Meteor.call("geoSearch",...,function(error,result){...}); with the correct set of arguments and a callback to fetch the result, it will call the $geoWithin stuff only on the server and do nothing on the client.

Upvotes: 1

Related Questions