Meteor.Collection with Meteor.bindEnvironment

你。 提交于 2019-12-24 04:24:08

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/22134897/meteor-collection-with-meteor-bindenvironment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!