从Firestore检索多个文档中的数据 [英] Retrieve data from multiple documents from Firestore
问题描述
我尝试了很多次,但是仍然无法从多个文档中检索数据.下面的屏幕快照显示我的收藏集中有3个文档,我的问题是如何从每个文档中检索数据.
I tried a lot of times but I am still not able to retrieve data from multiple documents, The below screenshot shows that I have 3 documents in my collection and my question is how to retrieve data from each of them.
我使用foreach循环遍历所有文档,但是我不知道如何从id RoKQBRcuaVfcGPKNswbx 或id Y8KbSQHcuxctJCJ1lWYH 中检索数据.最后一个ID.
I used foreach loop to loop through all the documents but I don't know how to retrieve data from id RoKQBRcuaVfcGPKNswbx or id Y8KbSQHcuxctJCJ1lWYH because it auto retrieve the data of the last id.
我真的需要你的帮助.谢谢.
I really need your help, please. Thank you.
推荐答案
快照
对象包含集合中的所有3个文档.您必须遍历所有内容并将数据呈现到HTML,如下所述.
The snapshot
object contains all the 3 documents in your collection. You must iterate over all and render data to your HTML as explained below.
db.collection("Policies List").get().then((snapshot) => {
const documents = snapshot.docs //array of documents
documents.forEach((doc) => {
const docData = doc.data() //Data of that single document
console.log(docData)
renderToHtml() // Code that creates new HTML elements
})
})
通过这种方式,您可以为集合中的所有文档创建新的HTML元素. renderToHtml()
函数将包含该 .innerHTML
代码.请确保在控制台中查看日志.他们将帮助您更好地了解结构.
This way you are creating new HTML elements for all the documents in your collection. The renderToHtml()
function will contain that .innerHTML
code.
Please make sure to see the logs in the console. They'll help understand the structure in a better way.
这篇关于从Firestore检索多个文档中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!