how to use the document id to access record in cloud firestore code example
Example 1: firestore get all documents in collection
// retrieve a collection
db.collection('documents')
.get()
.then(querySnapshot => {
const documents = querySnapshot.docs.map(doc => doc.data())
// do something with documents
})
// retrieve a document
db.collection('documents')
.doc(documentId)
.get()
.then(snapshot => {
const document = snapshot.data()
// do something with document
})
Example 2: get data from firestore
const [hospitalsDetails, setHospitalsDetails] = useState([])
useEffect(()=>{
//load hospitals into hospitalsList
const hospitals = []
db.collection('Hospitals').get()
.then(snapshot => {
snapshot.docs.forEach(hospital => {
let currentID = hospital.id
let appObj = { ...hospital.data(), ['id']: currentID }
hospitals.push(appObj)
hospitals.push(hospital.data())
})
setHospitalsDetails(hospitals)
})
},[])