I have an API call in the photoRetriever function. I saved the API object to the variable named "data" and I want to access this in the App class. I am trying to access an image in the API that is a method titled "url". It says that the variable data is not defined when I try to access it in the _generateMarkup method. What is the best way to define this variable so I can easily gain access to it outside of the function?
Code:
const photoRetriever = async function() {
try {
const res = await fetch('https://api.nasa.gov/planetary/apod?api_key=MHfiCXEcU8gFYbgux8MWX0nBFYRtzJsbefF4eq5R');
const data = await res.json();
console.log(data.url)
} catch(err) {
alert(err)
}
}
photoRetriever();
class App {
_parentEl = document.querySelector('.parent__el');
constructor() {
this._generateMarkup();
}
_generateMarkup() {
const markup = `
<div class="space__photo">
<img src="${data.url}" alt="" />
</div>
`;
this._parentEl.insertAdjacentHTML('afterbegin', markup);
}
}
const app = new App()