簡體   English   中英

Firebase 在 React Native 數據獲取中返回“未定義”值

[英]Firebase returning "undefined" value in React Native data fetch

我正在嘗試從 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 上的數據結構如下 -
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項目。

請嘗試以下操作:

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

undefined 是鍵,嘗試在鍵內迭代並檢索數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM