【问题标题】:How to display result from Meteor.HTTP.Get如何显示 Meteor.HTTP.Get 的结果
【发布时间】:2015-03-31 18:41:47
【问题描述】:

编辑:现在可以使用了。诀窍是将HTTP.get 移动到服务器端并使用simple:reactive-method 包从方法中获取结果。

我可以使用一些帮助来弄清楚如何显示Meteor.HTTP.Get 的结果。文档很粗略,这里没有与我的案例相关的主题。

我是searching Foursquare,在您附近寻找当地的农贸市场。然后在地图中显示结果(还没有地图)。代码如下:

起始页:

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

即将推出的地图页面。 编辑日期:2015 年 3 月 31 日

<template name="locatorMap">

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

</template>

路由(lib/router.js)

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

助手(client/locator/locator.js)。 编辑日期:2015 年 3 月 31 日

// 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 ...");
        }
    }
});

方法(server/methods/foursquare.js)。 编辑日期:2015 年 3 月 31 日

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 ...");
        }
    }
});

我可以在控制台上看到数据。但我只是不确定如何将它传递给模板助手。如果你们需要更多信息,请告诉我。

感谢任何帮助。谢谢!

【问题讨论】:

  • search转换成一个方法,并遵循here的建议。
  • 我已经修改了方法和助手。你能再看看吗? Meteor.http.get 的回调现在抛出一个未定义的。

标签: web-services meteor foursquare


【解决方案1】:

这个问题实际上只是:“我如何从助手调用方法?”,答案是herehere。但是,为了使这些解决方案起作用,您需要您的方法返回一个值,而不是进行异步 HTTP 调用(返回 undefined)。阻力最小的路径是仅在服务器上定义您的FoursquareSearch 方法(将其放在/server 目录下)并使用同步方法调用。例如:

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...");
      }
    }
  }
});

【讨论】:

  • 这行得通!谢谢!稍加说明,我忘记了 Geolocation 包在服务器端不起作用。所以我必须把它移到助手那里。
猜你喜欢
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
相关资源
最近更新 更多