【问题标题】:lose the last element of the array in for loop在for循环中丢失数组的最后一个元素
【发布时间】:2018-05-07 18:32:26
【问题描述】:

当我执行这段代码时,数组的最后一个元素丢失了,或者为什么?为什么会出现这种情况,会不会是因为它是一个异步函数?

async  getRow(){
  const user =  firebase.auth().currentUser
var itemsRef =  Metodos.getRef().child('Amigos/'+user.uid);
 itemsRef.on('value', async (snapshot) =>{

  let items =  [];

  childs = snapshot.val();
  for(child in childs){
    local = await Metodos.getLocal('-Ky7eG2rueIq47GUa7v5');
    firebase.database().ref().child('usuario/'+child).on('value',  (snap) =>{
    datos =  snap.val();

         items.push({
        nombre: datos.nombre,
        imagen: datos.imagen

      })


   })



  }
  this.setState({
    amigos: items
  })



});



}

【问题讨论】:

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


    【解决方案1】:

    看清楚你的图片,你的对象实际上有 5 个元素。

    就在您console.log 时,它有 4 个元素,Chrome 会拍摄该对象的快照,因此显示为 (4) [{...}, {...}, {...}, {...}]

    拍摄快照后它不会改变,但您可以在扩展区域中看到当前值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 2018-04-07
      相关资源
      最近更新 更多