2016-07-20 17 views
1

Meteor非同期メソッド内にコールバック関数を設定して、「読み取り可能な」イベントで呼び出すようにしました。しかし、コールバックはon "readable"が起動しているときにコールされていません(私が設定したconsole.logから起動していることがわかります)。Meteor jsコールバックが動作しない

ここに何か不足していますか?私はいくつかの異なることを試して、数時間それを行ってきました!

Meteor.startup(() => { 

    Meteor.call("getfeed", function(feedloader) { 
    //I get: TypeError: undefined is not a function] 
    console.log(feedloader); 
    }); 

}); 

Meteor.methods({ 

    getfeed: function(callb) { 

    var req = request('http://feeds.feedburner.com/Techcrunch'); 
    var feedparser = new FeedParser(); 
    testing = []; 

    //........a bunch of functions........ 

    feedparser.on('readable', function() { 

     var stream = this 
     , meta = this.meta 
     , item; 

     while (item = stream.read()) 
     { 
     //I'm pushing the results into testing var 
     testing.push(item); 
     } 

     //From the logs I can see that this is called 12 times 
     //but the callback's not firing!!! 

     console.log(testing.length); 
     callb(testing); 

    }); 
    } 
}); 

答えて

1

流星のメソッドを使用すると、メソッドを「コール」するとき、彼らはあなたがそれを通過していても、コールバックの引数を取得しないという意味では、非同期関数ではありません。代わりに、それぞれのメソッドは、Fiberの中で実行されます。これは、非同期コードを処理する別の方法です。

幸運なことに、Meteorには両方のスタイルを混在させる素晴らしいヘルパーがあります。あなたがする必要があるのは、メソッドコードの "純粋な"非同期部分をMeteor.wrapAsyncで囲むことです。この構造は多かれ少なかれこのように見えるはずです:

Meteor.methods({ 

    getfeed: function() { 
    var wrapped = Meteor.wrapAsync(function (callb) { 

     var feedparser = new FeedParser(); 
     testing = []; 

     // ... 

     feedparser.on('readable', function() { 
     // probably the same code you have, but without "callb()" 
     }); 

     feedparser.on('end', function() { 
     // NOTE: No error here, so the first argument must be null. 
     callb(null, testing); 
     }) 
    }); 

    // NOTE: Finally, call the wrapped function 
    return wrapped(); 
    } 
}); 
+0

こんにちは、私は '終わり'のイベントを持っていないということです。 '読み取り可能な'イベントはここで繰り返し止められるまで呼び出され、いつ停止するかわかりません!変数と同期して 'testing'変数を保持できる方法はありますか? – jaisonDavis

+0

申し訳ありませんが悪いです。終了イベントがあります。それを逃した! – jaisonDavis

+0

そして、それは返される必要がありますラップ();単にwrapped()の代わりに。 – jaisonDavis

関連する問題