2014-03-02 81 views
2

在一個函數,它已經內Meteor.binEnvironment,當我運行<collection>.find ({}),我得到的錯誤throw new Error ('Can \' t wait without a fiber '); 如果你把該呼叫也內Meteor.bindEnvironment(<collection>.find ({})),錯誤消息變成:throw new Error (noFiberMessage);Meteor.Collection與Meteor.bindEnvironment

有問題的功能通過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()); 
    }) 
}); 
+0

我不知道是否能解決您的實際問題,但在你的例子,沒有必要使用'流星.bindEnvironment',只需使用'Meteor.setTimeout'而不是'setTimeout'('Meteor.setTimeout'將爲你使用'Meteor.bindEnvironment')。 –

回答

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

你可以換任何異步功能,使之同步和與它這樣綁定纖維。

+0

我無法在我的項目中應用建議的解決方案,目前採用這種方式: 流星體方法(「teste」:Meteor.bindEnvironment(function(){){ var Fiber = Meteor.require('fibers')) ; VAR未來= Meteor.require( '纖維/未來'); VAR未來=新未來(); 的setTimeout(函數(){ 返回future.return( 光纖(函數(){ Sessions.findOne( {}); })。run() ); },15000); console.log('fut',future.wait()); }) }); – rogeriojlle

1

在我的項目,我不能申請推薦的解決方案,目前正在做的是這樣的:

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()); 
    }) 
}); 
相關問題