2

I am getting "deposited over undefined" result for current_game with this synthax when running node.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 = [];
        var unixtime = Math.round(new Date().getTime()/1000.0);
        mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`, `win`, `system`, `time`) VALUES (\''+offer.steamid_other+'\',\'Sorry, but you deposited too many items\',\'System\', \'0\', \'1\', \''+unixtime+'\')', function(err, row, fields) {});
        return;
    } else {
        mysqlConnection.query('SELECT `value` FROM `info` WHERE         `name`=\'current_game\'', 
        function(err, row, fields) {  
            var current_game = (row[0].value);
            mysqlConnection.query('SELECT COUNT(item) FROM `game' + current_game + '` AS maxitemperperson WHERE `userid`=\''+offer.steamid_other+'\'' , function(err, row) {  
                someVari = row[0].maxitemperperson;
                console.log('Deposited over ' +someVari);
                if((someVari + offer.items_to_receive.length) > 10) {
                    offers.declineOffer({tradeOfferId: offer.tradeofferid});                        
                    mysqlConnection.query('INSERT INTO `messages` (`userid`,`msg`,`from`, `win`, `system`, `time`) VALUES (\''+offer.steamid_other+'\',\'Sorry, but you deposited too many items\',\'System\', \'0\', \'1\', \''+unixtime+'\')', function(err, rowl, fields) {});
                    return;
                }
            })
        })
    }
});

Where is the error in the synthax? Basically, the first part till "else" is working, but afterwards I am trying to count the number of items from a certain table (which is generated dynamically through the current_game variable) that have the same userid and afterwards add another number of items and compare with 10 (which can also be written as row[0].value.

Any help would be appreciated.

Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
bagda391
  • 74
  • 1
  • 8

2 Answers2

1

someVari = row[0].maxitemperperson; is assigning undefined to someVari.

That means that row[0] exists but does not have a property named maxitemperperson (or, less likely, that the property exists but itself is set to undefined).

As a minimal debugging technique, you can use console.dir(row[0]); to find out what's going on.

You should also check that the query did not result in an error by examining the value of err in your callback.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • I did console.dir(someVari); and it's undefined .. is the sql statement wrong? – bagda391 Nov 10 '15 at 06:23
  • I wouldn't say wrong, rather incomplete. You only request `COUNT(item)` in your SQL statement, so the result only contains this. Change it to `SELECT COUNT(item),maxitemperperson ...` and it should work (assuming this column exists in the database). – Gerald Schneider Nov 10 '15 at 06:39
  • I want to attribute the variable the count(item) result. Maxitemperperson does not exist. It's just an alias for the result of the select statement. – bagda391 Nov 10 '15 at 06:41
  • Then `SELECT COUNT(item) AS maxitemperperson` should do it. – Gerald Schneider Nov 10 '15 at 06:43
  • @GeraldSchneider so what's the problem then..? – bagda391 Nov 10 '15 at 06:48
1

You have the AS alias in the wrong place in the SQL statement:

'SELECT COUNT(item) FROM `game' + current_game + '` AS maxitemperperson WHERE `userid`=\''+offer.steamid_other+'\''

This will alias the table name, so you could do COUNT(maxitemperperson.item) to reference it (useful if you have more than one table in the query).

What you want is this:

'SELECT COUNT(item) AS maxitemperperson FROM `game' + current_game + '` WHERE `userid`=\''+offer.steamid_other+'\''
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78