我从 firestore 获取了数据库,我希望在表中呈现所有用户。我从数据库中获取了所有用户数据并将其存储在一个数组中,但每次尝试映射时,我都会看到一个长度为 0 的空数组,而数组内有元素。
这是从firestore获取数据的代码
componentDidMount(){
let allUser = []
firestore.collection('user').get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
allUser.push(doc.data())
})
}).then(console.log(allUser.map((item, i) => console.log(item))))
.catch((error) => console.log(error))
}
最佳答案
您正在正确构建 allUser 数组,但随后您使用 console.log
映射它,但 console.log
没有显式返回某些内容,因此您删除了对象通过为每个项目返回未定义。
正如您在此处看到的,您的 console.log
将数组转换为 未定义
元素的集合。
只需在 forEach 循环中 console.log
你的数据(如果你需要的话)..
关于javascript - 我无法迭代从 firestore 获取的数据并将其移入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560743/