我正在尝试在Meteor中使用twit以便与Twitter REST API进行通信。
如果我自己调用它,它在/ server /目录中的server.js文件中运行正常。如果我从一个观察中包裹或调用它,或者甚至调用一个从观察中调用twit函数的函数,我会得到错误。
例如,这在/server/server.js中完全正常。
T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
console.log('error: ' + JSON.stringify(err,0,4));
console.log('reply: ' + JSON.stringify(reply,0,4));
});
但是,假设每次插入记录时我想说Twitter。
var query = Posts.find({}, {fields: {}});
var handle = query.observe({
added: function(post, before_index){
if(post.twitter_id_str === undefined || post.twitter_id_str === '' ||
post.twitter_id_str === null) {
T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
console.log('error: ' + JSON.stringify(err,0,4));
console.log('reply: ' + JSON.stringify(reply,0,4));
if(reply){
// TODO update record with twitter id_str
// BREAKS here - crash, restart
console.log('incoming twitter string: ' + reply.id_str);
Posts.update(
{_id: post._id},
{$set:{twitter_id_str:reply.id_str}}
);
}
});
} else {
console.log('remove me we have it: ' + post.twitter_id_str);
}
}
});
这会引发此错误,服务器崩溃并重新启动,但在我评论Break的情况下没有运行代码逻辑。
app/packages/mongo-livedata/collection.js:215
throw e;
^
Error: Meteor code must always run within a Fiber
at [object Object].get (app/packages/meteor/dynamics_nodejs.js:14:15)
at [object Object]._maybeBeginWrite (app/packages/mongo-livedata/mongo_driver.js:68:41)
at [object Object].update (app/packages/mongo-livedata/mongo_driver.js:191:20)
at [object Object].update (app/packages/mongo-livedata/collection.js:203:32)
at app/server/server.js:39:13
at /usr/lib/meteor/lib/node_modules/twit/lib/oarequest.js:85:16
at passBackControl (/usr/lib/meteor/lib/node_modules/twit/node_modules/oauth/lib/oauth.js:359:11)
at IncomingMessage.<anonymous> (/usr/lib/meteor/lib/node_modules/twit/node_modules/oauth/lib/oauth.js:378:9)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
Exited with code: 1
总而言之,Twitter代码可以自行运行,但不会在Meteor光纤内部运行。我试着把它放在另一个函数中并从观察中调用它......没有用。
任何建议或想法?
答案 0 :(得分:4)
您需要在光纤中执行twit post API调用:
Fiber(function(){...你的twit API调用...})。run()
查看此相关问题:"Meteor code must always run within a Fiber" when calling Collection.insert on server