我正在使用ntwitter模块从meteor应用程序内部访问推特的流node.js,但是当试图在回调函数内插入到集合中时,应用程序崩溃了:
twitter.stream('statuses/filter', {'track':'whatever'}, function(stream) {
stream.on('data', function (data) {
//logging the data coming back works fine
console.log(data);
//the next line throws "Error: Meteor code must always run within a Fiber"
Tweets.insert(data);
});
});
在Meteors线性执行模型的上下文中,有没有推荐的使用异步回调的方法?我试着将插入包在一个新的光纤中,似乎可以工作,但我不确定它可能会有什么影响。
我发现这个http://gist.io/3443021很有帮助,但我仍然不确定哪种方法适合我的特定情况,所以任何帮助都将不胜感激。
干杯
发布于 2013-01-12 22:53:27
我们使用了不同的设计模式。在异步回调中,我们的行为更像是设备驱动程序,只是将结果缓冲到内存中:
var tweets = [];
twitter.stream('statuses/filter', {'track':'whatever'}, function(stream) {
stream.on('data', function (data) {
//logging the data coming back works fine
console.log(data);
//the next line throws "Error: Meteor code must always run within a Fiber"
tweets.push(data);
});
});
然后,在纤程中的正常Meteor执行环境中,无论是在计时器中还是在函数的结果中,我们都会排空tweets数组,然后执行插入操作。Javascript数组并不关心它是否在纤程中运行。
在我们的例子中,我们使用异步IMAP电子邮件而不是tweet来做到这一点,但类比仍然成立。
发布于 2015-01-24 04:49:08
将回调封装在Meteor.bindEnvironment
中,如下所示:
twitter.stream('statuses/filter', {'track':'whatever'}, function(stream) {
stream.on('data', Meteor.bindEnvironment(function (data) {
//logging the data coming back works fine
console.log(data);
//the next line throws "Error: Meteor code must always run within a Fiber"
Tweets.insert(data);
}));
});
根据this SO post about Async wrappers on the Meteor Server的说法,当您使用第三方api/npm模块管理回调时,您希望使用Meteor.bindEnvironment
(看起来就是这样)。
Meteor.bindEnvironment
创建一个新的纤程,并将当前纤程的变量和环境复制到新纤程中。
https://stackoverflow.com/questions/14290286
复制相似问题