我有这个错误:
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()
的存储火库是好的。
你有主意吗?
首先,确保res对象具有items
,并且它必须是一个数组。然后,将forEach迭代器更改为map
,这样它就会根据res.items生成一个新的映射数组。
let promises = res.items.map( item => item.getDownloadURL() );
Promise.all(promises).then((downloadURLs) => {
this.setState({ urlImage: downloadURLs });
})
问题似乎出现在let promises = res.items.forEach( item => item.getDownloadURL());
行
forEach()
函数返回undefined。请改用map()
函数,该函数将返回结果数组。
let promises = res.items.map( item => item.getDownloadURL() );