Meteor.Collection with Meteor.bindEnvironment

2019-07-19 11:10发布

Within a function that is already within Meteor.binEnvironment, when I run <collection>.find ({}), I get the error throw new Error ('Can \' t wait without a fiber '); If you place that call also within Meteor.bindEnvironment(<collection>.find ({})), the error message becomes: throw new Error (noFiberMessage);

The function in question runs through Meteor.methods ({}) Where am I going wrong?

Example to reproduce the error:

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

2条回答
萌系小妹纸
2楼-- · 2019-07-19 11:57

Try using Meteor._wrapAsync instead.

This is an example of an async function, but any other would do:

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

var syncfunction = Meteor._wrapAsync(asyncfunction);

var result = syncfunction();

console.log(result);

You could wrap any asynchronous function and make it synchronous and bind the fibers with it this way.

查看更多
祖国的老花朵
3楼-- · 2019-07-19 12:07

I could not apply the suggested solution in my project, currently do this way:

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());
  })
});
查看更多
登录 后发表回答