【问题标题】:Firebase returning "undefined" value in React Native data fetchFirebase 在 React Native 数据获取中返回“未定义”值
【发布时间】:2019-12-13 05:05:52
【问题描述】:

我正在尝试从 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})
  });
}

Firebase上的数据结构如下-


数组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 项。

【问题讨论】:

    标签: javascript reactjs firebase react-native firebase-realtime-database


    【解决方案1】:

    尝试以下方法:

    getData(){
      firebase.database().ref('Restaurants/').on("value", snapshot =>{
      snapshot.forEach((childSub) => {
        let key = childSub.key;
        let restaurantList = childSub.val();
        console.log(restaurantList);
       });
      });
    }
    

    未定义是键,尝试在键内迭代并检索数据

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2020-03-15
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多