2
我正在嘗試在流星中使用twit以便與Twitter REST API交流。如何實現對不使用Meteor光纖的包的調用?
如果我自己調用它,可以在/ 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的代碼運行正常在它自己的而不是當流星纖維材料內。我試圖把它放在另一個函數中,並從觀察等內部調用它...無濟於事。
任何推薦或想法?
謝謝你。我向該主題添加了適用於我的解決方案的工具。希望它能幫助別人。 –