javascript - Meteor wrapAsync/Node Fiber Future 不工作

标签 javascript node.js meteor node-fibers meteor-methods

我正在尝试使用 Meteor Method 从一个 API 获取 json 数据,我尝试使用 MeteorwrappAsync 以及 NodeFuture。下面是我的代码:

模板助手 - 客户端

    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:使用 MeteorwrappAsync - 服务器端

'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:使用Node Fiber Future-服务器端

'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();


}

在这两种方法中,我都得到了在控制台中打印的值,但它们没有被返回。

以下是截图:Console Log

我不知道我错在哪里,请大家给我一些建议。

编辑:添加模板代码:

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

最佳答案

来自docs也就是说,您可以省略异步回调,它将同步运行。所以这应该在服务器上工作:

'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 性数据源。因此,要么使用 reactive-var 来存储结果,要么使用 Stubailo meteor-reactive-method 中的这个包。 .

请告诉我这是否能解决您的问题!

关于javascript - Meteor wrapAsync/Node Fiber Future 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37208590/

相关文章:

javascript - 如何使单选按钮在字段集中水平

java - npm 与 java - 抛出错误

javascript - 如何在 JavaScript 中保存数组的状态

javascript - 如何检查 Meteor Collection 中的 NotEquals/Exists?

javascript - 将 jQuery 幻灯片超大化到特定的 <div>

javascript - 如果字符串长度超过 12,如何放置...

node.js - 如何使用 Node.JS 请求 HTTP Digest 身份验证?

javascript - 如何使用jQuery获取动态表的输入字段值

javascript - 如何将 meteor collection.find() 与排序、跳过和限制一起使用

javascript - 无法让我的 if else 语句逻辑工作