Forgive my ignorance, but I've been bashing my head against the wall on this for two days now. I'm looking to make a request to a website and then do stuff with that data.
I've isolated my problem to the following: I pull the data with Request, but it doesn't seem to 'hold on' to the data. If I run the following code:
request = require('request');
request('https://api.btcmarkets.net/market/BTC/AUD/orderbook', function (error, response, body) {
let orderBookRequest = JSON.parse (body);
console.log(orderBookRequest);
});
This returns data, all is good with the world. But when I try to call the orderBookRequest outside of this function, the variable is not defined. As in this:
request = require('request');
request('https://api.btcmarkets.net/market/BTC/AUD/orderbook', function (error, response, body) {
let orderBookRequest = JSON.parse (body);
});
console.log(orderBookRequest);
Edit: Having done further reading, I don't think it's about synchronicity, but about block scope. Because running this:
request = require('request');
let orderBookRequest = undefined;
request('https://api.btcmarkets.net/market/BTC/AUD/orderbook', function (error, response, body) {
let orderBookRequest = JSON.parse (body);
});
setTimeout(function(){ console.log(orderBookRequest); }, 5000);
Still returns undefined.
I've been Googling and playing around, with no luck. What am I doing wrong?
Thanks in advance.