【问题标题】:Uncaught (in promise) TypeError: undefined is not iterableUncaught (in promise) TypeError: undefined is not iterable
【发布时间】:2021-01-08 05:44:20
【问题描述】:

我有这个错误:

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

我的代码:

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')
}
})

我在foreach(items)ref() 中有一项存储firebase 很好。

你有想法吗?

【问题讨论】:

    标签: reactjs firebase react-native promise setstate


    【解决方案1】:

    首先,确保你的 res 对象有items 并且它必须是一个数组。 然后,将您的 forEach 迭代器更改为 map,以便它根据您的 res.items 生成一个新的映射数组。

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

    【讨论】:

    • 好的,地图成功了!谢谢你 在承诺中,我忘记了 [ ] = Promise.all([promises]) 最后一个问题:现在,我有一个数组: [D] 0: D a: 2 b: null c: null f: null g: false h: false i: "firebasestorage.googleapis.com/v0/b" proto: Object length: 1 proto: Array(0) 我如何只得到路径?
    • 您应该使用已解决的项目作为返回值,而不是承诺本身。定义 [] = Promise.all 并使用该 [] 数组仅返回 promise 对象本身。你应该在分辨率数组上做任何你想做的事情,在你的情况下它是 downloadURLs。
    【解决方案2】:

    问题似乎出在let promises = res.items.forEach( item => item.getDownloadURL());这一行

    forEach() 函数 returns undefined。使用map() 函数代替returns the resulting array

    let promises = res.items.map( item => item.getDownloadURL() );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-06
      • 2021-08-18
      • 1970-01-01
      • 2017-04-13
      • 2015-09-18
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多