问题
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