我无法访问从 Firestore 快照保存的数组的内容。
这是代码:
function getDataFromFirestore() {
let myList = [];
db.collection("test")
.get()
.then((snapshot) => {
snapshot.forEach((doc) => {
myList.push(doc.data());
});
});
console.log(myList);
console.log(myList[1]);
console.log(myList.length);
}
getDataFromFirestore();
这里是console.log()的返回:
问题: 为什么使用forEach在数组中保存了3个项目,并且它的长度= 0?
如何访问数组中保存的对象?
非常感谢您的帮助。
最佳答案
db.collection("test").get()
返回一个 Promise,这意味着在最后的 console.logs 之后执行。
解决方案可能是将 console.logs 移到里面,或者使用 async wait。
关于javascript - Vanilla JS,尝试访问数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66501059/