Skip to content
Advertisement

Retrieve data from multiple documents from Firestore

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.

enter image description here

enter image description here

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.

Advertisement

Answer

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
  })
})

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement