mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.27k stars 2.52k forks source link

TypeError: Cannot read property '0' of undefined #1255

Closed bogdy2016 closed 8 years ago

bogdy2016 commented 8 years ago

ahr0cdovl3myos5wb3n0aw1nlm9yzy9rd2puem9kzwyvzmfzlnbuzw Help please!

Tradebot.js:


}       
                        if(offer.items_to_receive == undefined) return;             
                        mysqlConnection.query('SELECT `value` FROM `info` WHERE `name`=\'maxitems\'', function(err, row, fields) {
                            if(offer.items_to_receive.length > row[0].value) {
                                offers.declineOffer({tradeOfferId: offer.tradeofferid});
                                offer.items_to_receive = [];
                                mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`) VALUES (\''+offer.steamid_other+'\',\'toomuch\',\'System\')', function(err, row, fields) {});
                                return;
                            }
                        });
dougwilson commented 8 years ago

Hi! If you are getting that error from the code you posted, most likely it's because there is something in err and so there are no returned rows.

You can see a very simple example of code in the README at https://github.com/felixge/node-mysql#introduction for what you should do, at minimum, with the err argument. The standard callback pattern in Node.js means that if err is set, all other arguments will be undefined and you would have to act on that error in some way.

The most basic change you should make to your code above is to change it to the following:

if(offer.items_to_receive == undefined) return;             
mysqlConnection.query('SELECT `value` FROM `info` WHERE `name`=\'maxitems\'', function(err, row, fields) {
    if(err) throw err;
    if(offer.items_to_receive.length > row[0].value) {
        offers.declineOffer({tradeOfferId: offer.tradeofferid});
        offer.items_to_receive = [];
        mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`) VALUES (\''+offer.steamid_other+'\',\'toomuch\',\'System\')', function(err, row, fields) {});
        return;
    }
});