NodeJS TwitchAPI在线/离线状态

时间:2016-12-13 02:10:14

标签: javascript node.js httprequest twitch

我正在尝试检查Streams是在线还是离线...

但我遇到1个问题所有用户都不需要此请求..当1来自在线且1离线时,只有1或最大2 ...

这是一个例子: 在MySQL中有3个Streams 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:\ Bot> node test2.js

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

我花了2-3天试图解决它,但我仍然不知道......

1 个答案:

答案 0 :(得分:0)

结果是

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

因为你在异步函数里面调用rows[i].channels 异步函数也在循环中

见这个

在调用request2的回调之前,循环已经结束或i成为行,这就是为什么它总是只显示你的最后一个频道

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]);
        }
    }
});