Firebase request not executing properly

I am trying to execute a Firebase request and does not seem to work properly. I am trying to access the name of the school that I have in the Firebase database. The database is as follows:

Schools { Random School Name { schoolLocation: Random Location uid: random UUID } } 

If I use the following code to get information about the "Random School Name", I get a snapshot with a zero value:

 databaseReference.child("Schools").queryEqualToValue("Random School Name").observeSingleEventOfType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in print(snapshot) }) { (error: NSError) in self.displayError("Search Failed", message: "We couldn't search for that - \(error.code): \(error.localizedDescription)") } 

If I use this line of code, the query returns me the name of the school, as expected. I want to track if there is an error using observeSingleEventOfType :

 let query = databaseReference.child("Schools").queryEqualToValue("Florida Institute of Technology") print(query) 

Why is this not working?

+1
ios xcode swift firebase firebase-database
source share
2 answers

I think that here you are asking for a priority that will work only if your schools have priority (extremely unlikely).

If this is really a problem, solve it by explicitly specifying the order: queryOrderedByKey() . Then also make sure that the loop over the children is the result, as the query will return a list, even if there is only one result:

 databaseReference .child("Schools") .queryOrderedByKey() .queryEqualToValue("Random School Name") .observeSingleEventOfType(.Value, withBlock: { snapshot in for childSnapshot in snapshot.children { print(snapshot) } }) 
+1
source share

Hey Dan, you should try this query to get certain data.

 self. databaseReference.queryOrderedByChild("Schools").queryEqualToValue("Random School Name").observeSingleEventOfType(.Value, withBlock: { snapshot in print(snapshot.value) }) 

This query returns a random school name for key schools

+1
source share

All Articles