2017-10-22 26 views
0

これは品質などの必要なデータを保持するので、app_dataを取得しようとしていますが、app_dataをログに記録するたびに、未定義でログオンするたびにitemJsonのログが正常に機能しますか?NodeJS未定義のJSON値

コード

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

とすぐ

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

getBPPrice(item, itemJson); 

、にconsole.log(itemJson)。私が前に述べたように動作しますが、console.log(itemJson.app_data);単に未定義を出力するので、品質値である次の出力は機能しません。

JSON

https://pastebin.com/cFSd6GLU

ペーストビンリンク、それは非常に長いためには、App_Dataにはあまりにも底部付近にあります。

編集:私は取得

エラー -

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)

+1

[ 'JSON.stringify()']の二番目の引数(HTTPS://developer.mozilla .org/ja-jp/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)は「代替」です。変更を加えずに "pretty printing"しただけの場合は、実際に何にもマッチしない可能性があります(未定義はJSONの出力ではないため)。しかし、文字列からプロパティにアクセスすることはできないので、ここではいくつかの概念が間違っているようです。 –

答えて

2

それはあなたがここでやろうとしているものは100%明確ではないのですが、あなたは、配列theirItemstheirItemsの各メンバーが持っているように見えますあなたが望むオブジェクト。あなたがこれを行うとき、しかし...

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

itemJsonは今、あなたがしたいオブジェクトを表す文字列、ないオブジェクトそのものです。

itemJson.app_data 

のようにオブジェクトの個々の要素にアクセスすることはできません。実際のオブジェクトではなく文字列であるためです。

私は何がやりたいことは、単にオブジェクト自身を取得し、あなたの関数にそれを渡していると思う:

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