web services - How to display result from Meteor.HTTP.Get -
edit: got working now. trick move http.get
server-side , use simple:reactive-method
package result method.
i use figuring out how display result of meteor.http.get. docs sketchy , there's no topics here relates case.
i'm searching foursquare find local farmers & markets around you. display result in map (no map yet). here's 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
// 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 ..."); } } });
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 fetch ..."); } } });
i can see data on console. i'm not sure how how pass template helper. if guys need more info, let me know.
any appreciated. thx!
the question just: "how call method helper?", answered here , here. however, in order solutions work, you'll need method return value rather making asynchronous http call (which returns undefined
). path of least resistance define foursquaresearch
method on server (put under /server
directory) , use synchronous method invocation. example:
meteor.methods({ foursquaresearch: function(cat) { check(cat, string); var search_location = geolocation.latlng(); if (search_location) { try { // fill in blanks here params, timeout, etc. var result = http.get(...); return result.data.response; } catch (_error) { throw new meteor.error("no result", "failed fetch..."); } } } });
Comments
Post a Comment