meteor包asynchronous/节点光纤未来不工作

我试图从一个API使用meteor方法获取JSON数据,我试图使用meteorwrapAsync以及节点未来。 以下是我的代码:

模板助手 – 客户端

getLocationTimebyAPI: function (company_location) { Meteor.call('getLocationTimebyAPIServerMethod', company_location, function(error, results){ if(error){ console.log('error',error.reason); } else { var localtime = results.data.data.time_zone[0].localtime var utcoffset = results.data.data.time_zone[0].utcOffset console.log(localtime+ ' '+utcoffset); var returntext = localtime+' (UTC '+utcoffset+')'; return returntext; } }); } 

方法1:使用Meteor wrapAsync – 服务器端

 'getLocationTimebyAPIServerMethod': function(company_location){ var apiurl = 'http://api.worldweatheronline.com/free/v2/tz.ashx?q='+company_location+'&format=json&key=XXXXXX'; var convertAsyncToSync = Meteor.wrapAsync( HTTP.get ), resultOfAsyncToSync = convertAsyncToSync( apiurl ); return resultOfAsyncToSync; } 

方法2:使用节点光纤未来服务器端

 'getLocationTimebyAPIServerMethod': function(company_location){ // use the node fibers npm var Future = Npm.require('fibers/future'); var apiurl = 'http://api.worldweatheronline.com/free/v2/tz.ashx?q='+company_location+'&format=json&key=XXXXXXXX'; // Create our future instance. var future = new Future(); HTTP.get( apiurl, {}, function( error, response ) { if ( error ) { future.return( error ); } else { future.return( response ); } }); return future.wait(); } 

在这两种方法中,我正在获取打印在控制台中的值,但不会返回。

以下是截图: 控制台日志

我不知道我在哪里错了,谁能给我一些build议。

编辑:添加模板代码:

  <tr> <td>Local Time:</td> <td><input id="company_location_time" name="company_location_time" type="text" size="23" placeholder="Lead Company Location Time" value="{{getLocationTimebyAPI company_location}}" readonly style="background:#7FAAFF;font-weight:bold;"><p style="font-size:8px;">Local Time Powered By <a style="font-size:8px;" href="http://www.worldweatheronline.com/search-weather.aspx?q={{company_location}}" target="_blank">World Weather Online</a></p></td> </tr> 

从文档说,你可以忽略asynchronouscallback,它会同步运行。 所以这应该在服务器上工作:

 'getLocationTimebyAPIServerMethod': function(company_location){ // do checks var apiurl = 'http://api.worldweatheronline.com/free/v2/tz.ashx?q='+company_location+'&format=json&key=XXXXXX'; var result = HTTP.get( apiurl ); return result; } 

在客户端,模板助手应该返回undefined因为当助手被调用时没有返回值。 并且在助手中没有react native数据源会导致模板重新渲染。 所以,要么使用reactive-var来存储结果,要么使用stubailo meteor-reactive-method中的这个包。

让我知道如果这解决了你的问题!

我使用了上面提到的@tomsp的方法:

首先添加https://atmospherejs.com/simple/reactive-method

meteor加简单:反应法

然后更改我的代码

服务器端方法

 'getLocationTimebyAPIServerMethod': function(company_location){ // do checks var apiurl = 'http://api.worldweatheronline.com/free/v2/tz.ashx?q='+company_location+'&format=json&key=50a151065dc2a4ea69c1b93032805'; var result = HTTP.get( apiurl ); var localtime = result.data.data.time_zone[0].localtime var utcoffset = result.data.data.time_zone[0].utcOffset var returntext = localtime+' (UTC '+utcoffset+')'; return returntext; } 

客户端辅助函数

  getLocationTimebyAPI: function (company_location) { return ReactiveMethod.call("getLocationTimebyAPIServerMethod", company_location); } 

结果显示在下面的屏幕截图中:

在这里输入图像说明