我是 React 和 Firebase 的新手,我真的很难处理数组和对象我猜你不能将 .map
用于我的数据格式化(或类型)的方式。我查看了堆栈,但没有任何帮助(至少在我实现修复的不力努力中)。
我正在尝试使用 .map
来映射来自 firebase 的结果,但我收到错误 TypeError: this.state.firebasedata.map
不是函数。
获取数据:
componentWillMount(){
this.getVideosFromFirebase()
}
getVideosFromFirebase(){
var youtubeVideos = firebase.database().ref('videos/');
youtubeVideos.on('value', (snapshot) => {
const firebasedata = snapshot.val();
this.setState({firebasedata});
});
}
相关国家:
constructor(props){
super(props);
this.state = {
firebasedata: []
}
};
.map 在渲染中:
render(){
return(
<div>
{this.state.firebasedata.map((item) =>
<div key="{item}">
<p>{item.video.name}</p>
</div>
)}
</div>
);
}
最佳答案
这是因为 firebase
不将数据存储为数组,而是存储为对象。所以你得到的回应是一个对象。
Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.
阅读this详细了解为什么 firebase 将数据存储为对象。
要映射对象,你可以做类似的事情
Object.keys(myObject).map(function(key, index) {
console.log(myObject[key])
});
关于javascript - React.js .map 不是函数,映射 Firebase 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49664994/