Meteor外部API调用有效,但客户端获取未定义

时间:2014-11-18 02:28:17

标签: meteor

客户端:

Template.storeInfo.events({
    'click #btn-user-data': function(e) {
        Meteor.call('callApi', function(err, data) {
             $('#result').text(JSON.stringify(data, undefined, 4));
             console.log(data); //Not working!!
         });
    }
});

服务器端:

storeApi.prototype.retrieve = function(endpoint) {
  try {
    var gotiye = HTTP.get(endpoint);
    console.log(gotiye); //Works!
    return gotiye;
  } catch (err) {
    throw new Error("Failed to fetch call GET retrieve from store. " + err.message);
  }
};

storeApi.prototype.getStoreInfo = function() {
    var url = this.rootApiUrl.replace(this.endpointToken, 
        this.endpoints.store);
    this.retrieve(url);
};

Meteor.methods({
    callApi: function() {
        var stnvy = new storeApi(Meteor.user().services.store.accessToken);
        var data = stnvy.getStoreInfo();
        return data;
    }
});

为什么它在服务器端工作但在客户端无法使用?集合是使用它的唯一方法吗?

1 个答案:

答案 0 :(得分:0)

忘记在getStoreInfo函数return this.retrieve(url);返回它并且它有效! :)