js read firestore database code example
Example 1: firestore set a document
let data = {
name: 'Los Angeles',
state: 'CA',
country: 'USA'
};
// Add a new document in collection "cities" with ID 'LA'
let setDoc = db.collection('cities').doc('LA').set(data);
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)
})
},[])