【问题标题】:Steam trade bot - TypeError: Cannot read property '0' of undefinedSteam 交易机器人 - 类型错误:无法读取未定义的属性“0”
【发布时间】:2016-08-28 21:54:53
【问题描述】:

所以,我的处境很艰难。我目前正在尝试使用 Steam 机器人碰碰运气,但遇到了一个我自己似乎无法解决的问题。

首先,我想说这在我使用托管在其他地方的数据库时有效,但是当我使用在本地主机上运行的 MySQL 服务器时,就会出现此问题。

问题如下:

/home/bot1/node_modules/mysql/lib/protocol/Parser.js:82 throw err; ^ TypeError: Cannot read property '0' of undefined at Query._callback (/home/bot1/bot1.js:711:143) at Query.Sequence.end (/home/bot1/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24) at Query.ErrorPacket (/home/bot1/node_modules/mysql/lib/protocol/sequences/Query.js:94:8) at Protocol._parsePacket (/home/bot1/node_modules/mysql/lib/protocol/Protocol.js:271:23) at Parser.write (/home/bot1/node_modules/mysql/lib/protocol/Parser.js:77:12) at Protocol.write (/home/bot1/node_modules/mysql/lib/protocol/Protocol.js:39:16) at Socket. (/home/bot1/node_modules/mysql/lib/Connection.js:96:28) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10)

Query 回调导致的问题是:

someVar = rows[0].playersCount;

这部分的完整代码是:

mysqlConnection.query('SELECT COUNT(DISTINCT userid) AS playersCount FROM game' + current_game, function(err, rows){
    someVar = rows[0].playersCount;
    console.log('Current Players: ' +someVar);
    if(someVar == 2 && items.length > 0 && endtimer==-1) {
        console.log('Found 2 Players');
        endtimer = setTimeout(EndGame,GameTime*1000);
        mysqlConnection.query('UPDATEgamesSETstarttime=UNIX_TIMESTAMP() WHEREid` = \'' + current_game + '\'', function(err, row, fields) {});
    }
});

在这种情况下,当我运行查询 SELECT COUNT(DISTINCT userid) AS playersCount FROM game2 时,它会返回 playersCount 1

我很难找出导致问题的原因,非常感谢任何帮助。

【问题讨论】:

    标签: mysql sql node.js


    【解决方案1】:

    Cannot read property '0' of undefined 行的意思是您正在尝试访问未定义变量的属性“0”。查看您的代码,未定义的变量是rows,这意味着您的查询可能有问题。由于您没有在回调中检查err 变量,因此您无法判断。

    此类回调方法的良好做法是首先验证它是否正常(通过检查是否定义了err)。这将使您能够更好地了解出了什么问题,也可以让您的代码更好地处理错误:

    mysqlConnection.query('SELECT COUNT(DISTINCT userid) AS playersCount FROM game' + current_game, function(err, rows){
        if (err) {
           // first check if there's an error and mitigate it
           ...
           ...
           return;
        }
        // if we got here, there's no error, so we know rows is defined and can get to work
        someVar = rows[0].playersCount;
        console.log('Current Players: ' +someVar);
        if(someVar == 2 && items.length > 0 && endtimer==-1) {
            console.log('Found 2 Players');
            endtimer = setTimeout(EndGame,GameTime*1000);
            mysqlConnection.query('UPDATEgamesSETstarttime=UNIX_TIMESTAMP() WHEREid` = \'' + current_game + '\'', function(err, row, fields) {});
        }
    });
    

    【讨论】:

    • 我需要对您的代码进行任何更改吗?因为当我粘贴它时,我得到另一个无法阅读的错误
    • 嗯,你需要处理错误...把你的错误处理,而不是我的...
    猜你喜欢
    • 2021-10-01
    • 2020-10-28
    • 2019-09-17
    • 2021-03-01
    • 2022-11-30
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多