2016-12-13 17 views
0

私はストリームがオンラインまたはオフラインですチェックしようとしています...NodeJS TwitchAPIオフライン/オンライン状態

しかし、私は、この要求は、すべてのユーザーのために必要とされていない1つの問題を抱えている...のみ1または最大2 1から提示されたときそれをオンラインとオフラインの1である...ここで

は例です:MySQLでは は3つのストリームは test1の test2の test3に TEST4 (1、2、3と4は、ストリームである...)されている (すべてオフラインで私の出力はこれです...) test4はオフラインで、この4回です。

スクリプト:

var request = require('request'); 
var cheerio = require('cheerio'); 
var parser = require('json-parser'); 
var JSON = require('JSON'); 
var mysql = require('mysql'); 

var connection = mysql.createConnection({ 
    host  : 'localhost', 
    user  : 'status', 
    password : '', 
    database : 'all_channels' 
}); 

connection.query("SELECT * FROM channels", function(error, rows, response, fields) { 
    if (!!error) { 
     console.log('Error in Query!'); 
    } else { 
     for(var i in rows) { 


console.log(rows[i]) 
       request('https://api.twitch.tv/kraken/streams/'+rows[i].channels+'?client_id=(I have removed the ID from intent)', function (error, response, html) { 
     var profileyt = JSON.parse(html); 


if (profileyt["stream"] !== null) { 
         console.log(profileyt["stream"]["channel"]["status"]); 
        } else { 


request('https://api.twitch.tv/kraken/channels/'+rows[i].channels+'?client_id=(I have removed the ID from intent)', ', function (error, response, html) { 
          var profiletest = JSON.parse(html); 


console.log(''+profiletest["display_name"]+' is offline'); 
}) 
}})}}}); 

コンソール出力: C:\ボット>ノードが、私はそれを修正しようとして2〜3日を過ごしてきたが、私はまだ分からない

RowDataPacket { channels: 'test1', status: 'offline' } 
RowDataPacket { channels: 'test2', status: 'offline' } 
RowDataPacket { channels: 'test3', status: 'offline' } 
RowDataPacket { channels: 'test4', status: 'offline' } 

Test4 ist offline 
Test4 ist offline 
Test4 ist offline 
Test4 ist offline 

をtest2.js ...

+0

もちろん、forループ内で非同期関数を実行しているからです – Beginner

答えて

0

結果は

Test4 ist offline 
Test4 ist offline 
Test4 ist offline 
Test4 ist offline 
です

あなたは、ループ内で

はループがすでに終了しているかrequest2のコールバックが持って前にiが行になり、この

を参照され 非同期関数である非同期関数で内部rows[i].channelsを呼び出しているので、それは、常にそれが別々の機能

を作成し解決するために、あなたの最後のチャネル

for (var i in rows) { 
     request1('blahblah...', function(error, response, html) { 
      // rows[i].channels this will always displayed your last row data 
      request2(rows[i].channels, function (error, response, html) { 
      }) 
     }) 
    } 
} 

を表示されている理由であると呼ばれて

function getChannelStatus(row) { 
    // now the row will be pass is the correct one istead of the last row like your previous code 
    request('https://api.twitch.tv/kraken/streams/' + row.channels + '?client_id=(I have removed the ID from intent)', function(error, response, html) { 
      var profileyt = JSON.parse(html); 


      if (profileyt["stream"] !== null) { 
       console.log(profileyt["stream"]["channel"]["status"]); 
      } else { 


       request('https://api.twitch.tv/kraken/channels/' + row.channels + '?client_id=(I have removed the ID from intent)', function (error, response, html) { 
        var profiletest = JSON.parse(html); 


        console.log('' + profiletest["display_name"] + ' is offline'); 
       }) 
     } 
    }) 
} 

connection.query("SELECT * FROM channels", function(error, rows, response, fields) { 
    if (!!error) { 
     console.log('Error in Query!'); 
    } else { 
     for (var i in rows) { 

      console.log(rows[i]) 
      getChannelStatus(rows[i]); 
     } 
    } 
});