Hope all is well. Using node w fetch & trying to 'Get' the contents of a JSON file to be used in other functions in my script & possibly other scripts. Until now, i can only use them inside the fetch scope which is not optimal. pretty new to this but been workin on it for a couple days, read/watched a ton of tutorials, & searched here as well w no luck:-( for instance i'd like to do something like:
fetch (json){ }
let thePrice = data.price;
let theAmntSold = data.price;
....then later in script be able to use data.price & theAmntSold ie
totalAmntEarned(thePrice, theAmntSold) {
return thePrice * theAmntSold;
}
here is the latest I came up with and no luck
let theSystem, themaxPrice,
theblockPrices = [],
mainObj = {};
let showObj = function() {
//asyncronous so we need to set up for loop in a function
for (let prop in mainObj) {
console.log(prop);
console.log(mainObj[prop]);
}
};
fetch(URL, {
method: 'GET',
headers: {
Authorization: AUTH_KEY
}
}).then(function(resp) {
return resp.json();
}) //assign variables below
.then(function(data) {
showObj();
let theblockPrices = data.blockPrices;
console.log(data.maxPrice);
console.log(theblockPrices[0]);
});
so basically, i'd like to be able to use 'theblockPrices' in another function or variable. Also, when i call the showObj() inside the fetch block, I get no output in the console. Any tips would be amazing!
thanks