【发布时间】:2018-04-03 09:51:58
【问题描述】:
我正在尝试获取 app_data,因为它包含我需要的其他数据,例如质量,但每次我尝试记录 app_data 时都会出现未定义,记录 itemJson 工作正常吗?
代码
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
Pastebin 链接,因为它很长,app_data 也在底部附近。
编辑:
我得到错误 -
未定义 C:\Users\datpe\Desktop\d\bot.js:89 console.log(itemJson.app_data.quality); ^
TypeError:无法读取未定义的属性“质量” 在 getBPrice (C:\Users\datpe\Desktop\d\bot.js:89:32) 在 processOffer (C:\Users\datpe\Desktop\d\bot.js:136:4) 在 TradeOfferManager.manager.on (C:\Users\datpe\Desktop\d\bot.js:189:2) 在 emitOne (events.js:115:13) 在 TradeOfferManager.emit (events.js:210:7) 在 received.forEach (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\polling.js:235:10) 在 Array.forEach (本机) 在 getOffers (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\polling.js:219:12) 在 Helpers.checkNeededDescriptions (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\index.js:483:4) 在 Async.map (C:\Users\datpe\node_modules\steam-tradeoffer-manager\lib\assets.js:171:4)
【问题讨论】:
-
JSON.stringify()的第二个参数是“替换器”。如果您只是在“漂亮打印”之后没有修改,那么您需要undefined代替,因为它实际上最不可能匹配任何东西(因为 undefined 并不是真正的 JSON 输出)。但是你不能从一个字符串中访问一个属性,所以你这里似乎有几个概念是错误的。
标签: javascript json node.js