【问题标题】:Meteor.Collection with Meteor.bindEnvironmentMeteor.Collection 与 Meteor.bindEnvironment
【发布时间】:2014-04-03 19:13:03
【问题描述】:

在 Meteor.binEnvironment 中已经存在的函数中,当我运行 <collection>.find ({}) 时,我收到错误 throw new Error ('Can \' t wait without a fiber '); 如果您将该调用也放在Meteor.bindEnvironment(<collection>.find ({})) 中,则错误消息将变为:throw new Error (noFiberMessage);

有问题的函数贯穿Meteor.methods ({}) 我哪里错了?

重现错误的示例:

Meteor.methods({
  "teste" : Meteor.bindEnvironment(function(){
    var Future = Meteor.require('fibers/future');
    var future = new Future();
    setTimeout(function(){
      return future.return(Sessions.findOne({}))
    }, 15000);
    console.log('fut', future.wait());
  })
});

【问题讨论】:

  • 我不知道这是否解决了您的实际问题,但在您的示例中,无需使用Meteor.bindEnvironment,只需使用Meteor.setTimeout 而不是setTimeoutMeteor.setTimeout 将使用Meteor.bindEnvironment 为您服务)。

标签: meteor node-fibers


【解决方案1】:

尝试改用Meteor._wrapAsync

这是一个异步函数的示例,但任何其他函数都可以:

var asyncfunction = function(callback) {
    Meteor.setTimeout(function(){
        callback(null, Sessions.findOne({}))
    }, 15000);
}

var syncfunction = Meteor._wrapAsync(asyncfunction);

var result = syncfunction();

console.log(result);

您可以包装任何异步函数并使其同步并以这种方式绑定纤程。

【讨论】:

  • 我无法在我的项目中应用建议的解决方案,目前这样做:Meteor.methods({ "teste" : Meteor.bindEnvironment(function(){ var Fiber = Meteor.require('fibers'); var Future = Meteor.require('fibers/future'); var future = new Future(); setTimeout(function(){ return future.return( Fiber(function(){ Sessions.findOne({}); }).run() ); }, 15000); console.log('fut', future.wait()); }) });
【解决方案2】:

我无法在我的项目中应用建议的解决方案,目前这样做:

Meteor.methods({
  "teste" : Meteor.bindEnvironment(function(){
    var Fiber = Meteor.require('fibers');
    var Future = Meteor.require('fibers/future');
    var future = new Future();
    setTimeout(function(){
      return future.return(
        Fiber(function(){
          Sessions.findOne({});
        }).run()
      );
    }, 15000);
    console.log('fut', future.wait());
  })
});

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 2013-08-19
    • 2014-11-03
    • 1970-01-01
    • 2013-04-06
    • 2014-04-12
    • 1970-01-01
    • 2015-09-29
    • 2018-04-07
    相关资源
    最近更新 更多