Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get data from firestore returning metadata

Firebase version 9.6.9

I’m using onSnapshot function to get the data from the firestore.

collections

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code:

  const [posts, setPosts] = useState([])

  useEffect(() => {
    return onSnapshot(
      query(collection(db, 'posts'), orderBy('timestamp', 'desc')),
      (snapshot) => {
        console.log(snapshot.docs)
      }
    )
  }, [db])

snapshot.docs logs a metadata and not the data I need (the posts collection).

Log

>Solution :

The snapshot.docs is an array of QueryDocumentSnapshot. You can get data from your documents, by using .data() method on every snapshot as shown below:

return onSnapshot(
  query(collection(db, 'posts'), orderBy('timestamp', 'desc')),
  (snapshot) => {
    const result = snapshot.docs.map((d) => ({
      id: d.id,
      ...d.data()
    }))
    console.log('>>> Documents', result)
  }
)

The metadata has some information about the snapshots such as the source.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading