【发布时间】:2020-01-04 04:04:38
【问题描述】:
我正在为我的 React Native 应用程序编写代码,但我无法从 firebase.firestore().collection("test_data").doc(ID) 循环之外的 firebase 返回中获取数据。每当我在循环之后检查 dataArray 变量时,它都是空的。如果我在循环中检查它,数据就在那里。我认为这是一个范围问题,但我就是不明白。我也不能在循环内调用任何用户定义的函数。
try {
let dataArray = [];
// get the document using the user's uid
firebase.firestore().collection("users").doc(uid).get()
.then((userDoc) =>{
// if the document exists loop through the results
if (userDoc.exists) {
data = userDoc.data().saved_data; // an array store in firebase
data.forEach(ID => { // loop through array
firebase.firestore().collection("test_data").doc(ID).get()
.then((doc) => {
dataArray.push(doc.data().test_data);
console.log(dataArray) // the data shows
})
console.log(dataArray) // the data does not show
})
}
})
}
catch (error) {
}
}
【问题讨论】:
标签: javascript reactjs firebase google-cloud-firestore