JD2017 JD2017 - 1 year ago 57
iOS Question

Query Not Working Firebase Swift

I have a Firebase that its structured as follows:

  • "zipcodes"

    • 12345

      • 1

        • "city": "test"

        • "state": "XX"

    • 98765

      • 2

        • "city": "test2"

        • "state": "AA"

      • 3

        • "city": "test3"

        • "state": "BB"

I am trying to query my database by the user's zip code; however, when I try to view the snapshot that it found, it is null. Here is the code I currently have:

func firebaseSearch(zipCode: String) {

let conditionRef = FIRDatabase.database().reference().child("zipcodes")

let query = conditionRef.queryEqualToValue(zipCode)

query.observeSingleEventOfType(.Value, withBlock: {snapshot in


for child in snapshot.children {



When I run the code, I get this:

(/zipcodes {
ep = 12345;
sp = 12345;

My database has around 77k entries, so I was worried iterating through all entries and trying to find the child with my zipCode value would take a large amount of time, so I am trying to use the query feature to see if it's any quicker. I appreciate any help, thank you very much!

Answer Source

You don't need query for this, use .child(zipCode) instead of .queryEqualToValue(zipCode). That will return the expected result.

Hope this helps!!