get data from firebase firestore code example
Example 1: get data in from a collection firestore
db.collection("users").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
});
Example 2: get one document based on id in firestore
//retrieve one document and save it to userDetails
const [userDetails, setUserDetails] = useState('')
db.collection('users').doc(id).get()
.then(snapshot => setUserDetails(snapshot.data()))
Example 3: 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)
})
},[])