Search code examples
reactjsfirebasereact-nativepromisesetstate

Uncaught (in promise) TypeError: undefined is not iterable


I have this error:

bundle.js:2066 Uncaught (in promise) TypeError: undefined is not iterable.

My code:

firebase.auth().onAuthStateChanged(user => {
  if (user)
  {
    if (user.uid != null)
    {
      let storageRef = firebase.storage().ref("Users/" + user.uid);
      console.log(storageRef)
      storageRef.listAll().then( res => {
        let promises = res.items.forEach( item => item.getDownloadURL() );
    
        Promise.all(promises).then((downloadURLs) => {
            this.setState({ urlImage: downloadURLs });
        })
      })
    }

    console.log(this.state.urlImage)

    const actionPhoto = { type: "TOGGLE_PHOTO", value: this.state.urlImage };
    this.props.dispatch(actionPhoto)

    const actionUid = { type: "TOGGLE_UID", value: user.uid };
    this.props.dispatch(actionUid)

    // On ajoute le nom et la photo du current user
    const actionName = { type: "TOGGLE_NAME", value: user.displayName };
    this.props.dispatch(actionName)

    //this.props.navigation.navigate('Navigation')
}
})

I have one item in foreach(items) and ref() storage firebase is good.

Have you got an idea?


Solution

  • First of all, make sure your res object has items and it must be an array. Then, change your forEach iterator to map, so it generates a new mapped array based on your res.items.

    let promises = res.items.map( item => item.getDownloadURL() );
    
    Promise.all(promises).then((downloadURLs) => {
                this.setState({ urlImage: downloadURLs });
            })