Firebase Query not Executing Properly

I am trying to execute a Firebase query and it doesn’t seem to be working properly. I am trying to access the name of a School I have in the Firebase Database. The database looks like this:

Schools {

    Random School Name {

        schoolLocation: Random Location
        uid: random UUID

    }
}

If I use the following code to get the info of the “Random School Name”, I get a snapshot of null:

  • App Delegate crashes because of Firebase initialization
  • Firebase Notifications through server-side code
  • How can I delete a specific child node in Firebase from UITableViewCell using Swift
  • Read Data from Firebase and Save Into an Array (Swift)
  • Firebase chat Push Notifications
  • How to update firebase query?
  • 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, however, the query gives me the name of the school back, as I would expect. I want to be able to track if there is an error using observeSingleEventOfType:

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

    Why isn’t this working?

    2 Solutions Collect From Internet About “Firebase Query not Executing Properly”

    I think you’re querying by priority here, which will only work if your schools have a priority (highly unlikely).

    If that is indeed the problem, solve it by being explicit about the ordering: queryOrderedByKey(). Then also be sure to loop over the children in the result, since a query will return a list, even if there’s only one result:

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

    Hey Dan you should try this query for retrieving specific data

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

    This query returns the random school name for the key schools