I'm having trouble identifying the issue here. I am fetching some json data (using a text file) and trying to push it into an array in the global scope. Even though I iterate over the json and push entries to the array, when I log it out, it appears empty. It seems like a simple mistake, but any help would be greatly appreciated.
let charities = [];
fetch('../mapdata/markers.txt').then(response => {
if (response.ok) {
return response.json();
}
throw new Error('Network response error.');
})
.then(charData => {
console.log(`inside: ${JSON.stringify(charData, null, 2)}`);
charData.map(entry => {
return charities.push(entry);
});
})
.catch(error => {
console.log('There has been a problem: ', error.message);
});
console.log(`outside: ${JSON.stringify(charities, null, 2)}`);
As I type this, I can't help but wonder if this might be an asynchronous issue...