Answers for "query data from firestore"

11

how to query in firestore

db.collection("cities").where("capital", "==", true)
    .get()
    .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });
Posted by: Guest on May-28-2020
2

get data from firebase firestore

useEffect(() => {
	db.collection('posts')
      .orderBy('timestamp', 'asc') // optional
      .onSnapshot((snapshot) => {
		setPosts(
			snapshot.docs.map((doc) => {
				return doc.data();
			})
		);
	});
}, []);
Posted by: Guest on June-15-2021
0

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)
        })
    },[])
Posted by: Guest on June-28-2020

Browse Popular Code Answers by Language