javascript - 仅服务器上的 meteor 异步问题

标签 javascript meteor

所以我在服务器文件夹中有一个像这样的方法:

dataHandler: function(id)
{
    var data;

    // here I call a server method
    Meteor.call('getData', id, function(err, result)
    {
        data = result;
    });

    // now I want to use data in another method
    Meteor.call('convertData', data, function(err, result)
    {
        data = result;
    });

    return data;
}

现在这不起作用,因为调用 Meteor.call('convertData' ... ) 时的 data 将是未定义的。我该如何在无法使用 Session.set/get 的服务器端的 Meteor 中完成这项工作?

最佳答案

在服务器上,您可以选择同步调用方法。如果您不提供默认的回调函数。

If you do not pass a callback on the server, the method invocation will block until the method is complete. It will eventually return the return value of the method, or it will throw an exception if the method threw an exception. - http://docs.meteor.com/#meteor_call

你的代码应该像这样工作:

dataHandler: function(id) {
  var data = Meteor.call('getData', id );
  return Meteor.call('convertData', data );
}

关于javascript - 仅服务器上的 meteor 异步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23666070/

相关文章:

javascript - 使用 Mailgun 从 Base64 字符串发送 pdf 附件

javascript - 如何在 JavaScript 中使用日历

javascript - 不正确的日期工具提示线加条形图 nvd3.js

javascript - 我如何访问函数外的数组元素

node.js - meteor 部署错误

django - 如何使用 django-ddp

javascript - 使用桥时,PhantomJS page.evaluate 返回未定义

javascript - 数据表 - 从页脚计算中省略某些行

javascript - 从集合中移除绑定(bind)以从 View 中移除

javascript - Meteor:获取发布日期