当我执行此代码时,数组的最后一个元素丢失或为什么?为什么会发生这种情况,是因为它是一个异步函数?
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
})
});
}
在你的照片上清楚地看,你的物体中实际上有5个元素。
就在你console.log
它有4个元素,而Chrome拍摄该对象的快照所以显示(4) [{...}, {...}, {...}, {...}]
。
拍摄快照后,它不会改变,但您可以在扩展区域中看到当前值。