2017-05-13 37 views
-1

所以我的工作有点与和的NodeJS蒸汽API,试图进行交易机器人 我需要知道哪些项目是收到,但是当我做,我得到一些JSON对象是不确定的

undefined 

这里的JSON:

{ 
    "partner":{ 
     "universe":1, 
     "type":1, 
     "instance":1, 
     "accountid":369917733 
    }, 
    "id":"2112720622", 
    "message":"No need to accept, just testing out a bot.", 
    "state":3, 
    "itemsToGive":[ 

    ], 
    "itemsToReceive":[ 
     { 
     "appid":730, 
     "contextid":"2", 
     "assetid":"10168993563", 
     "classid":"1989300153", 
     "instanceid":"302028390", 
     "amount":1, 
     "missing":true, 
     "icon_url":"IzMF03bi9WpSBq-S-ekoE33L-iLqGFHVaU25ZzQNQcXdB2ozio1RrlIWFK3UfvMYB8UsvjiMXojflsZalyxSh31CIyHz2GZ-KuFpPsrTzBG0qe6yD3n-ZDLdYXKOTVxqTudcPGjZrGLx5ujCF2nASeh-EF9XdfEAo2JLNZiKahtv0oMVu2u_0UdyEhk6f9BKZAarxm1OMLh9m3IWGBD1M58", 
     "icon_url_large":"IzMF03bi9WpSBq-S-ekoE33L-iLqGFHVaU25ZzQNQcXdB2ozio1RrlIWFK3UfvMYB8UsvjiMXojflsZalyxSh31CIyHz2GZ-KuFpPsrTzBG0qe6yD3n-ZDLdEC3YDlltU-cLND7c92Hxt-SVFGqfFe54SgACdfcFoDJPP82AOBtrhdZZ-Ga9zhwzDhgvNMZJfACpx2EfJbQ1xDhPJM5amiekAf1NktA", 
     "icon_drag_url":"", 
     "name":"Sealed Graffiti | Still Happy (Desert Amber)", 
     "market_hash_name":"Sealed Graffiti | Still Happy (Desert Amber)", 
     "market_name":"Sealed Graffiti | Still Happy (Desert Amber)", 
     "name_color":"D2D2D2", 
     "background_color":"", 
     "type":"Base Grade Graffiti", 
     "tradable":true, 
     "marketable":true, 
     "commodity":true, 
     "market_tradable_restriction":7, 
     "fraudwarnings":[ 

     ], 
     "descriptions":[ 
      { 
       "type":"html", 
       "value":"This is a sealed container of a graffiti pattern. Once this graffiti pattern is unsealed, it will provide you with enough charges to apply the graffiti pattern <b>50</b> times to the in-game world.", 
       "app_data":"" 
      }, 
      { 
       "type":"html", 
       "value":" ", 
       "app_data":"" 
      }, 
      { 
       "type":"html", 
       "value":"", 
       "color":"00a000", 
       "app_data":{ 
        "limited":"1" 
       } 
      } 
     ], 
     "owner_descriptions":[ 

     ], 
     "actions":[ 
      { 
       "name":"Inspect in Game...", 
       "link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D9253357711394385702" 
      } 
     ], 
     "market_actions":{ 
      "0":{ 
       "name":"Inspect in Game...", 
       "link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D9253357711394385702" 
      } 
     }, 
     "tags":[ 
      { 
       "internal_name":"CSGO_Type_Spray", 
       "name":"Graffiti", 
       "category":"Type", 
       "category_name":"Type" 
      }, 
      { 
       "internal_name":"normal", 
       "name":"Normal", 
       "category":"Quality", 
       "category_name":"Category" 
      }, 
      { 
       "internal_name":"Rarity_Common", 
       "name":"Base Grade", 
       "category":"Rarity", 
       "color":"b0c3d9", 
       "category_name":"Quality" 
      }, 
      { 
       "internal_name":"Tint5", 
       "name":"Desert Amber", 
       "category":"SprayColorCategory", 
       "category_name":"Graffiti Color" 
      } 
     ], 
     "id":"10168993563", 
     "owner_actions":[ 

     ], 
     "market_marketable_restriction":0 
     } 
    ], 
    "isOurOffer":true, 
    "created":"2017-05-13T09:50:24.000Z", 
    "updated":"2017-05-13T09:50:48.000Z", 
    "expires":"2017-05-27T09:50:24.000Z", 
    "tradeID":"1915564581201455085", 
    "fromRealTimeTrade":false, 
    "confirmationMethod":0, 
    "escrowEnds":null, 
    "rawJson":"{\n\t\"tradeofferid\": \"2112720622\",\n\t\"accountid_other\": 369917733,\n\t\"message\": \"No need to accept, just testing out a bot.\",\n\t\"expiration_time\": 1495878624,\n\t\"trade_offer_state\": 3,\n\t\"items_to_receive\": [\n\t\t{\n\t\t\t\"appid\": \"730\",\n\t\t\t\"contextid\": \"2\",\n\t\t\t\"assetid\": \"10168993563\",\n\t\t\t\"classid\": \"1989300153\",\n\t\t\t\"instanceid\": \"302028390\",\n\t\t\t\"amount\": \"1\",\n\t\t\t\"missing\": true\n\t\t}\n\t],\n\t\"is_our_offer\": true,\n\t\"time_created\": 1494669024,\n\t\"time_updated\": 1494669048,\n\t\"tradeid\": \"1915564581201455085\",\n\t\"from_real_time_trade\": false,\n\t\"escrow_end_date\": 0,\n\t\"confirmation_method\": 0\n}" 
} 

我可以轻松搞定合作伙伴,身份证,信息,状态,isOurOffer和所有的时间的事情,但是当我试图让itemsToRecieve,它只是抛出未定义

的JSON通过事件收集和上面的是我收到

这是该事件的确切JSON:

manager.on('sentOfferChanged', function(offer, oldState) { 
    var state = offer.state; 
    if(state == 3) { 
     console.log("Offer " + offer.id + " was accepted"); 
     console.log("[Offer #" + offer.id + "] The following items was recieved: "); 
     console.log(offer.itemsToReceive); 
    } 
}); 

它为什么要这么做只为对象之一?

+0

没有足够的信息。由于JSON是有效的,并且您说您使用的是流,所以我唯一想到的是该流尚未完成。您尚未收到整个流。 – JuanGG

+0

显示您的请求。我猜异步问题。 –

+0

@JuanGG我从来没说过我用过流 –

回答

0

也许你可以使用for来记录itemToReceive的每个项目。

事情是这样的:

manager.on('sentOfferChanged', function(offer, oldState) { 
    var state = offer.state; 
    if(state == 3) { 
     console.log("Offer " + offer.id + " was accepted"); 
     console.log("[Offer #" + offer.id + "] The following items was recieved: "); 

     for(var i in itemstoReceive) 
     { 
      var name = itemsToReceive[i].name; 
      console.log(name); 
      }} 
});