2017-01-22 4 views
0

複数の非同期応答が空であるために失敗していると判明したDiscordのボットを作成しました。それは8つの要求だけを行うように見え、他の要求は無視します。Nodejs複数の非同期要求が即座に失敗する

コードからの抜粋の実行:12のIDがあるので、すぐに失敗

/*Required modules 
    npm install request //used because it supports HTTPS 
    npm install cheerio 
*/ 
var request = require('request'); 
var cheerio = require('cheerio'); 

var ids = [ 
     "16286", 
     "16296", 
     "16284", 
     "15563", 
     "15964", 
     "15123", 
     "15592", 
     "868", 
     "15626", 
     "15627", 
     "339", 
     "350" 
    ]; 

var print = ""; 
var count = 0; 
for(var x=0;x<ids.length;x++) { 
    request({uri: "https://www.futbin.com" + "/17/player/"+ids[x], gzip: true}, function(error, response, full) { 
     let $ = cheerio.load(full); 
     var bins = $('.lowest_bin_next_tr'); 
     var header = $('.player_header').text().trim().split("-"); 
     console.log(header); 
     if(header != '') { 
     print += header[1].trim() + ", " + header[0] + "OVR " + $('.pcdisplay-pos').text().trim() + "\r\n"; 
     print += "**PS4:** " + $('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n"; 
     print += "**XBOX:** " + $('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n"; 
     } 
     if(++count == ids.length) 
      callback(print); 
    }); 
} 

4の要求を。なぜこれは8つの要求を満たすだけですか?

+0

エラーをCONSOLE.LOG試してみて、要求が –

+0

を失敗している理由は、ホストサーバがサービスまたはレート制限違反の拒否など、あなたの迅速な火災12のリクエストを見ることができました確認してください。正確なエラーとネットワークトレースを調べて、障害の内容を正確に確認する必要があります。また、あなたのリクエストがどんな順序でも返ってくることを知りたいかもしれません。そのため、 'print'変数でそれらを取得する順序はランダムにすることができます(ホストサーバーの特定の動作に依存します)。 – jfriend00

答えて

0

私はあなたのコードを実行するときに、返すために最初の4つの応答は空header変数を作成します。結果:

$('.player_header').text().trim().split("-"); 

は空の文字列です。

しかし、私がそれを実行するたびに、私は空の異なる4つを取得します。私は、これが12回の急速発射要求を送信するとき、これが何らかのサーバー側の問題だと思う。

テストでは、リクエストを1秒あたり1リクエストにまで遅らせると、すべてのレポートデータが12個になります。これは間違いなく多くの要求を一度に得ることを好まないサーバー側の問題です。

ここでは、リクエストを1秒あたり1秒遅らせるテストコードの例を示します。 (私は厳密にそれらをシリアライズ場合、それはまた働く、で飛行中の1つの要求

/*Required modules 
    npm install request //used because it supports HTTPS 
    npm install cheerio 
*/ 
var request = require('request'); 
var cheerio = require('cheerio'); 

var ids = [ 
     "16286", 
     "16296", 
     "16284", 
     "15563", 
     "15964", 
     "15123", 
     "15592", 
     "868", 
     "15626", 
     "15627", 
     "339", 
     "350" 
    ]; 

var print = ""; 
var count = 0; 
ids.forEach(function(id, x) { 
    setTimeout(() => { 
     request({uri: "https://www.futbin.com" + "/17/player/"+id, gzip: true}, function(error, response, full) { 
      if (error) { 
       console.log(error); 
      } else { 
       let $ = cheerio.load(full); 
       var bins = $('.lowest_bin_next_tr'); 
       console.log(`header data [${id}]`, `"${$('.player_header').text().trim()}"`); 
       var header = $('.player_header').text().trim().split("-"); 
       if(header != '') { 
        print += header[1].trim() + ", " + header[0] + "OVR " + $('.pcdisplay-pos').text().trim() + "\r\n"; 
        print += "**PS4:** " + $('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n"; 
        print += "**XBOX:** " + $('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n"; 
       } 
       if(++count == ids.length) 
        callback(print); 
      } 
     }); 
    }, x * 1000); 
}); 

function callback(data) { 
    console.log(data); 
} 

をFYI:私は、これは、生産コードで示唆、しかし、あなたに成功した1秒あたりの要求を行うコードを見せたかったありませんよ時)このように:

/*Required modules 
    npm install request //used because it supports HTTPS 
    npm install cheerio 
*/ 
var Promise = require('bluebird'); 
var request = Promise.promisify(require('request'), {multiArgs: true}); 
var cheerio = require('cheerio'); 



var ids = [ 
     "16286", 
     "16296", 
     "16284", 
     "15563", 
     "15964", 
     "15123", 
     "15592", 
     "868", 
     "15626", 
     "15627", 
     "339", 
     "350" 
    ]; 

Promise.mapSeries(ids, function(id) { 
    return request({uri: "https://www.futbin.com" + "/17/player/"+id, gzip: true}).then(function(data) { 
     var response = data[0]; 
     var full = data[1]; 
     let $ = cheerio.load(full); 
     var bins = $('.lowest_bin_next_tr'); 
     console.log(`header data [${id}]`, `"${$('.player_header').text().trim()}"`); 
     var header = $('.player_header').text().trim().split("-"); 
     let print = ""; 
     if(header != '') { 
      print += header[1].trim() + ", " + header[0] + "OVR " + $('.pcdisplay-pos').text().trim() + "\r\n"; 
      print += "**PS4:** " + $('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n"; 
      print += "**XBOX:** " + $('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n"; 
     } 
     return print; 
    }); 
}).then(function(results) { 
    console.log(results); 
});  
+0

@ inuyasha555 - FYI、それは私が厳密に一度に飛行中のリクエスト(私の答えのコードの2番目のブロック)にリクエストを厳密にシリアル化する場合にも機能します。この第2のスキームはまた、要求の順序で整列する応答の順序を保持する。 – jfriend00

1
'strict mode'; 
var async = require('async'), 
request = require('request'); 

var cheerio = require('cheerio'); 

var ids = [ 
    "16286", 
    "16296", 
    "16284", 
    "15563", 
    "15964", 
    "15123", 
    "15592", 
    "868", 
    "15626", 
    "15627", 
    "339", 
    "350" 
]; 

var print = ""; 

async.eachSeries(ids , idsIteration , finishIteration); 

function idsIteration(id , callBack){ 
    processId(id , callBack); 
} 


function processId(id , cb){ 
    request({uri: "https://www.futbin.com" + "/17/player/"+id, gzip: true}, function(error, response, full) { 
    if(error){ 
     console.log('there was error entertainting request :' + error); 
     cb(error); 
    } 
    else { 
     var c = cheerio.load(full); 
     var bins = c('.lowest_bin_next_tr'); 
     var header = c('.player_header').text().trim().split("-"); 
     console.log(header); 
     if(header != '') { 
     print += header[1].trim() + ", " + header[0] + "OVR " + c('.pcdisplay-pos').text().trim() + "\r\n"; 
     print += "**PS4:** " + c('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + c('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n"; 
     print += "**XBOX:** " + c('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + c('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n"; 
     } 
     cb(); 
     } 
    }); 
} 
function finishIteration(){ 
    console.log('all ids processed'); 
} 

私のコンソールに出力:enter image description here

+0

これはなぜ問題を解決するのですか? – jfriend00

+0

これはリクエストとそのいくつかをあなたの答えではなく、別のやり方でフラッシュします。 @ jfriend00 –

+0

あなたは "flush"という言葉で私を混乱させました。おそらくあなたが言うことは、これが飛行中に一度に1つしか持たない要求を連載することでしょうか?あなたはそれが動作するかどうかを確認しようとしましたか? – jfriend00

関連する問題