SF Dev
SF Dev

Reputation: 409

Query Not Working Firebase Swift

I have a Firebase that its structured as follows:

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

        print(snapshot.value)

        for child in snapshot.children {
            print(child)
        }

    })

}

When I run the code, I get this:

 (/zipcodes {
 ep = 12345;
 sp = 12345;
 })
 Optional(<null>)

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!

Upvotes: 0

Views: 309

Answers (1)

Ravi Prakash Verma
Ravi Prakash Verma

Reputation: 1484

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

Hope this helps!!

Upvotes: 1

Related Questions