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

How to get values of a field in Firestore in React

I use a document in firestore that stores three fields with boolean values. This boolean values I need for further processsing. In my programm I want to read this values. But how to read the fields of a document? As explained here it should work like following:

const getStateEntries = async () => {
  await getDoc(doc(db, 'Collection_Name', 'Document_ID'))
  .then((docSnap)=> {
    let alertField = docSnap.data['alert_state']
    console.log(alertField)
  })
  setAlertState(alertField)
}

When I run the code it looks like get no access to the field. When I print the variable to the console, I get an 'undefined'.

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

>Solution :

The data is not a property on DocumentSnapshot. Try refactoring the code as shown below:

let alertField = docSnap.data().alert_state
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