Samuel Stubbings
Samuel Stubbings

Reputation: 31

NodeJS Undefined JSON Value

I'm trying to get app_data because this holds other data that I need, such as quality, but every time I try to log app_data it comes up with undefined, logging itemJson works fine?

Code

function getBPPrice(item, itemJson){
     console.log(itemJson);
     console.log(itemJson.app_data);
     console.log(itemJson.app_data.quality);
     console.log(Prices.response.items[item].prices[itemJson.app_data.quality][itemJson.tradable].Craftable[0].value);
}

and

var item = theirItems[i].market_name;
var itemJson = JSON.stringify(theirItems[i], null, 4);

getBPPrice(item, itemJson);

Now, console.log(itemJson); works as I've stated before, but console.log(itemJson.app_data); just outputs undefined, so the next output which is the quality value doesn't work.

Json

https://pastebin.com/cFSd6GLU

Pastebin link because it is quite long, app_data is near the bottom too.

Edit:

Error i get -

undefined C:\Users\datpe\Desktop\d\bot.js:89 console.log(itemJson.app_data.quality); ^

TypeError: Cannot read property 'quality' of undefined at getBPPrice (C:\Users\datpe\Desktop\d\bot.js:89:32) at processOffer (C:\Users\datpe\Desktop\d\bot.js:136:4) at TradeOfferManager.manager.on (C:\Users\datpe\Desktop\d\bot.js:189:2) at emitOne (events.js:115:13) at TradeOfferManager.emit (events.js:210:7) at received.forEach (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\polling.js:235:10) at Array.forEach (native) at getOffers (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\polling.js:219:12) at Helpers.checkNeededDescriptions (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\index.js:483:4) at Async.map (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\assets.js:171:4)

Upvotes: 0

Views: 143

Answers (1)

Mark
Mark

Reputation: 92461

It's not 100% clear what you are trying to do here, but it looks like you have an an array theirItems and each member of theirItems is an object that you want. But when you do this…

var itemJson = JSON.stringify(theirItems[i], null, 4);

itemJson is now a string representing the object you want, not the object itself. You can't then access individual elements of the object like:

itemJson.app_data

because its a string, not the actual object.

I think what you want to do is simply grab the object itself and pass that to your function:

var itemJson = theirItems[i]
// then pass the actual object:
getBPPrice(item, itemJson);

Upvotes: 2

Related Questions