0
my problem is that I can’t return the results['name']
of the api request. When I click on the button it returns the Response.data with the following response
['Object, Object'] and when I Seto data.Results returns the error : scripts.js:15 Uncaught (in Promise) Typeerror: Cannot read Property 'name' of Undefined at scripts.js:15
The idea is : once I click the button, I return to a random planet.
If anyone can help I’d appreciate it.
let button = document.querySelector('#botao');
let name = document.querySelector('#Name');
function getPlanet() {
let randomNumber = Math.floor(( Math.random() * 88 ) + 1 )
let apiUrl = 'https://swapi.co/api/planets/' + randomNumber
// integrando API para adicionar a função getPlanet;
axios.get (apiUrl).then(( async function ( response ) {
console.log(response)
updateInfo ( await response.data.results['name'])
} ))
}
function updateInfo(data) {
name.innerText = data.results
}
// Evento Click;
button.addEventListener('click', getPlanet);