I have a promise from Fetch that I use to get the list of countries from our API:
async fetchDataFromAPI () {
console.log('fetchDataFromAPI')
let response = await fetch(process.env.NEXT_PUBLIC_BRIDGEMAN_API_URL + '/countries',
{
method: 'GET',
headers: {
'Content-Type': 'application/ld+json',
'Accept': 'application/json'
}
})
let data = await response.json();
return data
}
I call that promise inside of a class component in componentDidMount:
componentDidMount() {
this.setState({countries: this.fetchDataFromAPI()})
}
Then when I pass it to the render function, Like this:
const {
countries
} = this.state
console.log('state countries', countries)
I get the following output in my console:
Promise { <state>: "pending" }
<state>: "fulfilled"
<value>: Array(244) [ {…}, {…}, {…}, … ]
[0…99]
[100…199]
[200…243]
length: 244
<prototype>: Array []
<prototype>: Promise.prototype { … }
All the data is there in the ''. I just dont know how to get at it?
I've tried countries.then((data) => {console.log('data', data)})
but I get the message that countries has no method .then
. I've tried countries.map
and that is the same error.
How can I loop through and output the data on the page?