1

Edit: Got it working now. The trick is to move the HTTP.get to the server-side and use the simple:reactive-method package to get result from a method.

I could use some help figuring out how to display the result of Meteor.HTTP.Get. The docs are sketchy and there's no topics here that relates to my case.

I'm searching Foursquare to find local farmers & markets around you. then display the result in a map (no map yet). Here's the code:

The start page:

<template name="locator">

    <a class="button" href="{{pathFor route='locatorMap' query='group=farmers'}}">Farmers</a>
    <a class="button" href="{{pathFor route='locatorMap' query='group=markets'}}">Markets</a>

</template>

The soon-to-be map page. Edited: Mar 31, 2015

<template name="locatorMap">

    <div class="list">
        {{#each venues}}
            <p>{{name}}. {{location.lat}}, {{location.lng}}</p>
        {{/each}}
    </div>

</template>

The routing (lib/router.js)

Router.route('/locator', {name: 'locator'});
Router.route('/locator/map', {name: 'locatorMap'});

The helper (client/locator/locator.js). Edited: Mar 31, 2015

// A static list of venue categories
Foursquare.categoryId = { ... };

Template.locatorMap.helpers({
    venues: function() {
        var search_group = Router.current().params.query.group;
        var search_categories = Foursquare.categoryId[search_group].join(',');
        var search_location = Geolocation.latLng();

        if (search_location) {
            // using simple:reactive-method
            return ReactiveMethod.call('FoursquareSearch', search_categories, search_location);
        } else {
            throw new Meteor.Error("No Location", "Failed to get ...");
        }
    }
});

The method (server/methods/foursquare.js). Edited: Mar 31, 2015

Meteor.methods({
    FoursquareSearch: function(categories, location) {
        check(categories, String);
        check(location, Object);

        try {
            var search_result = HTTP.call(
                'GET', 'https://api.foursquare.com/v2/venues/search?',
                {
                    timeout: 5000,
                    params: { ... }
                }
            );
            return search_result.data.response.venues;
        } catch (_error) {
            throw new Meteor.Error("No Result", "Failed to fetch ...");
        }
    }
});

I can see data on the console. But i'm just not sure how how to pass it into a template helper. If you guys need more info, just let me know.

Any help is appreciated. Thx!

RZKY
  • 300
  • 1
  • 4
  • 15
  • 1
    Convert `search` into a method and follow the advice [here](http://stackoverflow.com/questions/28640400/get-meteor-method-return-value-inside-helper-function). – David Weldon Mar 29 '15 at 03:24
  • i've revised the method and helper. can you have a look again? the callback from Meteor.http.get now trows an undefined. – RZKY Mar 30 '15 at 09:46

1 Answers1

4

The question is really just: "How do I call a method from a helper?", which is answered here and here. However, in order for those solutions to work, you'll need your method to return a value rather than making an asynchronous HTTP call (which returns undefined). The path of least resistance is to define your FoursquareSearch method only on the server (put it under the /server directory) and to use a synchronous method invocation. For example:

Meteor.methods({
  FoursquareSearch: function(cat) {
    check(cat, String);

    var search_location = Geolocation.latLng();

    if (search_location) {
      try {
        // fill in the blanks here with params, timeout, etc.
        var result = HTTP.get(...);
        return result.data.response;
      } catch (_error) {
        throw new Meteor.Error("No Result", "Failed to fetch...");
      }
    }
  }
});
Community
  • 1
  • 1
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • this works! thx! with a little note, i forgot that the Geolocation package don't work on the server side. so i have to move it to the helper. – RZKY Mar 31 '15 at 10:43