seishun / node-steam-trade

Node.js wrapper around Steam trading
162 stars 36 forks source link

Cannot read property 'is_currency' of undefined #50

Closed kjsmita6 closed 9 years ago

kjsmita6 commented 9 years ago

I made a bot which adds refined metal to trade when someone adds a key. So I added a key, and the bot crashed with this error.

TypeError: Cannot read property 'is_currency' of undefined
    at SteamTrade.addItem (C:\Users\Kyle\node_modules\steam-trade\index.js:324:1
8)
    at addRec (C:\Users\Kyle\Desktop\Steam Bots\Crate\Bot 2\index.js:326:13)
    at SteamTrade.bot.setPersonaState.Steam.EPersonaState (C:\Users\Kyle\Desktop
\Steam Bots\Crate\Bot 2\index.js:343:6)
    at SteamTrade.emit (events.js:98:17)
    at SteamTrade._onTradeStatusUpdate (C:\Users\Kyle\node_modules\steam-trade\i
ndex.js:184:14)
    at Request._callback (C:\Users\Kyle\node_modules\steam-trade\index.js:243:10
)
    at Request.self.callback (C:\Users\Kyle\node_modules\steam-trade\node_module
s\request\request.js:236:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (C:\Users\Kyle\node_modules\steam-trade\node_modules\
request\request.js:1142:14)
    at Request.emit (events.js:117:20)

I am not entirely sure what this error message means nor what the 'is_currency' property is. This is the code that cause the error:

function addRec() {
    steamTrade.addItem(recAmt[i]);
    logger.debug('Added a rec');
    myMetalAdded += (i*rec);
}

Where 'recAmt' is

recAmt = backpack.filter(function(item) {
                return item.name === 'Reclaimed Metal';
            });

Any help would be appreciated. Thanks.

Ax6 commented 9 years ago

are you sure that recAmt is an array of items?

kjsmita6 commented 9 years ago

I was fairly sure that's what backpack.filter returned, but I could be wrong. If it isn't an array, what is it?

fiskWasTaken commented 9 years ago

Prepend console.log(recAmt) before the addItem call

See what you get

kjsmita6 commented 9 years ago

I get [] from that.

Ax6 commented 9 years ago

Here you go, array is empty, so you are passing an undefined element which has no properties and throw the error. Maybe you don't have any metal in your inventory? Or backpack.filter doesn't work as you thought. (Agree with Fiskie ↓)

fiskWasTaken commented 9 years ago

Honestly, it seems you should be refactoring your code a bit - recAmt is a global variable here I assume? It can lead to unexpected behaviour when dealing with asynchronous environments like node.

kjsmita6 commented 9 years ago

I've fixed the issue by adding a few if/else statements. Thanks everyone for the help.