0

Attempting to build multi-dimensional array. After pushing 2 objects to array, console is logging responses-all : [], when I would like it to contain multiple objects.

When I log the array within the funtion, console is correctly logging the multi-dimensional array:

responses - gdax: [{
    bitstamp: {
      timestamp: '2017-05-15T16:41:07Z',
      error: '',
      data: [Object]
    }
  },
  {
    gdax: {
      timestamp: '2017-05-15T16:41:13Z',
      error: '',
      data: [Object]
    }
  }
]

But not when it logs responses-all at the end of the script.

I also find it strange that the console is logging in reverse. responses-all is logging first, then responses-bitstamp, then responses-gdax, while in fact I have them in the code in reverse order.

Here is full code:

router.get('/cryptox', function(req, res) {
  var responses = [];

  var Cryptox = require("cryptox");

  var gdax = new Cryptox("gdax");
  var bitstamp = new Cryptox("bitstamp");

  gdax.getTicker({
    pair: "BTC_USD"
  }, function(err, ticker) {
    if (!err)

      responses.push({
        'gdax': ticker
      });
    console.log('responses-gdax : ' + util.inspect(responses));

  });

  bitstamp.getTicker({
    pair: "BTC_USD"
  }, function(err, ticker) {
    if (!err)

      responses.push({
        'bitstamp': ticker
      });
    console.log('responses-bitstamp : ' + responses);

  });
  res.json(responses);
  console.log('responses-all : ' + util.inspect(responses)); //responses-all : []
});
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
chuckieDub
  • 1,767
  • 9
  • 27
  • 46

0 Answers0