2012-05-06 1 views
2

私はTwitterのREST APIと通信するためにMeteorでtwitを使用しようとしています。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); 
    } 
} 
}); 

このエラーをスローすると、サーバーがクラッシュして再起動しますが、ブレークにコメントしたコードロジックは実行されません。

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コードはそれ自身ではうまく動作しますが、流星繊維の中ではうまく動作しません。私はそれを別の機能に入れてみて、観察などの中からそれを呼び出そうとしました...無駄です。

おすすめまたはご意見はありますか?

答えて

4

あなたは繊維でなじるポストAPI呼び出し実行する必要があります:。

ファイバー(関数(){...あなたのなじるのAPIコールを...})(実行)

がありますこの関連する質問をご覧ください。"Meteor code must always run within a Fiber" when calling Collection.insert on server

+1

ありがとうございます。私はそのスレッドに私のソリューションのために働くものを加えました。うまくいけばそれは他人を助けるでしょう。 –

関連する問題