firebase get data code example

Example 1: unity get data from firebase

// Perform the steps at the following link to setup Firebase
// for your Unity project:
https://firebase.google.com/docs/unity/setup

// To connect to the databse in a script, use the following
// two lines:
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOURPROJECTNAME.firebaseio.com/");
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;

// To get a data snapshot and take action with it:
FirebaseDatabase.DefaultInstance.GetReference("DATABASENAME").GetValueAsync().ContinueWith(task => {
	if (task.IsFaulted)
	{
		// Failure
	}
	else if (task.IsCompleted)
	{
		DataSnapshot snapshot = task.Result;
		// Success
	}
});

Example 2: get data in from a collection firestore

db.collection("users").get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
    });

Example 3: collection get firesotre

async getMarker() {
    const snapshot = await firebase.firestore().collection('events').get()
    return snapshot.docs.map(doc => doc.data());
}

Example 4: 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)
        })
    },[])