2017-10-22 1 views
0

품질과 같이 필요한 다른 데이터를 보유하고 있기 때문에 app_data을 얻으려고합니다. 그러나 app_data를 로깅하려고 할 때마다 undefined로 표시되며 itemJson 로깅이 제대로 작동합니까?NodeJS Undefined 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 너무 아래쪽에 있습니다.

편집 : 내가 얻을

오류 - 그것은 당신이 여기서 뭘 하려는지 100 % 분명하지만,이 배열 theirItems을 가지고 theirItems의 각 구성원 인 것 같습니다

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

두 번째 인수 :

난 당신이 단순히 개체 자체를 잡고 함수에이 전달됩니다 뭘 원하는지 생각 //developer.mozilla을 .org/ko-ko/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)는 "대체 자"입니다. 수정없이 "예쁜 인쇄"를하면, 실제로는 아무것도 일치하지 않으므로 (정의되지 않은 것은 실제로 JSON 출력이 아니기 때문에) 대신에 '정의되지 않음'을 원할 것입니다. 그러나 문자열에서 속성에 액세스 할 수 없으므로 여기에 몇 가지 개념이 잘못 표시 된 것 같습니다. –

답변

2

네가 원하는 대상. 이 작업을 수행 할 때 ...

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

itemJson 지금 당신이 할 개체가 아니라 개체 자체를 나타내는 문자열입니다. 다음과 같이 객체의 개별 요소에 액세스 할 수 없습니다.

itemJson.app_data 

문자열은 실제 객체가 아니기 때문에 액세스 할 수 없습니다. (HTTPS를 [`JSON.stringify()`]에

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