- The first call (Storage.list('s3 bucket path')) returns a list of keys.
- For each key, I make a second call (Storage.get('key')) that returns a url
- Inside the first AND second API call I console.log and get the correct data I need for each.
- this.setState works perfectly inside the first API call, but it doesn't inside the second one.
My code is:
state = { keyItems: [], urlItems: [] }
componentDidMount() {
Storage.list('')
.then(keyItems => {
console.log("keyItems: ", keyItems)
this.setState({ keyItems: keyItems })
/*for each photo key in the folder..*/
keyItems.forEach(function(keyItem) {
/*get the URL*/
Storage.get(keyItem.key)
.then(urlItem => {
console.log("urlItem: ", urlItem)
/*add URL item to state*/
/*ANY CALL TO this.setState HERE DOESN'T WORK, i've tried
even simple tests*/
this.setState(prevState => ({ urlItems:[...prevState.urlItems, { key: keyItem.key, source: urlItem } ]}))
})
.catch(err => {
console.log("error fetching photo URL", err)
})
})
})
.catch(err => {
console.log("error fetching photos keys", err)
})
}
- I've tried making the calls from arrow functions outside the componentDidMount but still can't access the this.setState
Thanks!