Firebase 在 React Native 数据提取中返回“未定义”值
2019-12-13
780
我正在尝试从 Firebase 实时数据库中获取餐厅列表,
我的代码片段用于获取数据 -
_isMounted = false;
state :{
data: [],
}
constructor(props){
super(props)
this.state = {
data:[],
}
}
componentDidMount(){
this._isMounted = true
this.getData()
}
componentWillUnmount(){
this._isMounted = false
}
getData(){
firebase.database().ref('Restaurants/').on("value", snapshot =>{
let restaurantList = snapshot.val();
console.log(restaurantList)
this.setState({data: restaurantList})
});
}
数组
restaurantList
的控制台输出为 -
Array [
undefined,
Object {
"location": "Testing data",
"name": "Time Traveller",
"rating": 4.5,
"tags": "Indian, Asian",
},
Object {
"locations": "Testing data",
"name": "Novelty",
"rating": 4.5,
"tags": "Indian",
},
]
我不确定从哪里获取数组中的
undefined
项。
1个回答
尝试以下操作:
getData(){
firebase.database().ref('Restaurants/').on("value", snapshot =>{
snapshot.forEach((childSub) => {
let key = childSub.key;
let restaurantList = childSub.val();
console.log(restaurantList);
});
});
}
未定义是键,尝试在键内进行迭代并检索数据
Peter Haddad
2019-12-13