Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Steam trade bot - TypeError: Cannot read property '0' of undefined

Tags:

sql

node.js

mysql

So, I'm in a rough spot. I'm currently trying my luck with a steam bot, yet I'm encountering a issue I seem to be unable to solve myself.

First, I would like to say that this works when I'm using a database hosted elsewhere, but when I'm using the MySQL server running on localhost, this issue is appearing.

The issue is the following:

/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)

The issue that the Query callback is leading to is:

someVar = rows[0].playersCount;

The full code for this part is:

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

When I run the query SELECT COUNT(DISTINCT userid) AS playersCount FROM game2 in this case, it returns with playersCount 1

I'm having a hard time to see what causes the issue, any help is much appreciated.

like image 377
Dead poet Soceity Avatar asked Dec 12 '25 21:12

Dead poet Soceity


1 Answers

what the line Cannot read property '0' of undefined means is that you are trying to access the property '0' of an undefined variable. from looking at your code, the undefined variable is rows, which means you probably have some issue with your query. Since you don't check the err variable in your callback, you can't tell.

The good practice for such callback methods is to first verify it went well (by checking if err is defined). this will allow you to understand better what went wrong, and also for your code to better handle errors:

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) {});
    }
});
like image 53
Nir Levy Avatar answered Dec 15 '25 10:12

Nir Levy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!