-2

I have an object trade returned from Steam's API GetTradeOffer which contains the following:

tradeofferid=697052768, accountid_other=38044877, expiration_time=1443176120, trade_offer_state=7

if I console.log(trade) I get:

tradeofferid=697052768, accountid_other=38044877, expiration_time=1443176120, trade_offer_state=7

however if I do console.log(trade.trade_offer_state) I get undefined

Has anyone got any idea as to why?

EDIT:

Here is the full markup for this section:

function checkOffer(offer) {
    offers.getOffer({
        tradeOfferId: offer.trade_id.toString()
    }, function(err, trade) {
        if (err) {
            throw err;
        }
        logger.info(trade);
        if (trade.trade_offer_state == '7' || trade.trade_offer_state == '4' || trade.trade_offer_state == '5' || trade.trade_offer_state == '6' || trade.trade_offer_state == '9' || trade.trade_offer_state == '10') {
            connection.query('UPDATE `trade_queue` SET `offer_sent` = 0 WHERE `id` = ' + "'" + offer.id + "'", function(err, rows, fields) {
                if (err) {
                    throw err;
                }
                logger.info(rows[0]);
            });
        }

    });
}
Risadinha
  • 16,058
  • 2
  • 88
  • 91
Ricky Barnett
  • 1,130
  • 3
  • 15
  • 32

3 Answers3

1

If your object is:

var trade = {
    "response": {
        "offer": {
            "tradeofferid": "697052768",
            "accountid_other": 38044877,
            "expiration_time": 1443176120,
            "trade_offer_state": 7
        }
    }
}

Then you get your value using:

var val = trade.response.offer.trade_offer_state; // 7

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0
var trade = { 
    "tradeofferid": 697052768, 
    "accountid_other": 38044877, 
    "expiration_time": 1443176120, 
    "trade_offer_state": 7 
};

console.log( trade.trade_offer_state ); // same as
console.log( trade["trade_offer_state"] ); // both return 7
karkael
  • 431
  • 2
  • 9
0

When var trade = "tradeofferid=697052768, accountid_other=38044877, expiration_time=1443176120, trade_offer_state=7";

You can add these lines:

function(err, trade) {
    eval.bind( this )( "var " + trade );
    trade = { 
        "tradeofferid": tradeofferid, 
        "accountid_other": accountid_other, 
        "expiration_time": expiration_time, 
        "trade_offer_state": trade_offer_state
    };
    if (err) {
        throw err;
    }
    logger.info(trade);
    if (trade.trade_offer_state == '7' || trade.trade_offer_state == '4' || trade.trade_offer_state == '5' || trade.trade_offer_state == '6' || trade.trade_offer_state == '9' || trade.trade_offer_state == '10') {
        connection.query('UPDATE `trade_queue` SET `offer_sent` = 0 WHERE `id` = ' + "'" + offer.id + "'", function(err, rows, fields) {
            if (err) {
                throw err;
            }
            logger.info(rows[0]);
        });
    }
}

OR

function(err, trade) {
    eval( trade );
    if (err) {
        throw err;
    }
    logger.info(trade);
    if (trade_offer_state == '7' || trade_offer_state == '4' || trade_offer_state == '5' || trade_offer_state == '6' || trade_offer_state == '9' || trade_offer_state == '10') {
        connection.query('UPDATE `trade_queue` SET `offer_sent` = 0 WHERE `id` = ' + "'" + offer.id + "'", function(err, rows, fields) {
            if (err) {
                throw err;
            }
            logger.info(rows[0]);
        });
    }
}
karkael
  • 431
  • 2
  • 9
  • Forthemore, you can simply this line : `if( [7,4,5,6,9,10].indexOf( trade_offer_state ) != -1 ){ //...`. – karkael Sep 11 '15 at 11:17