Latcie Latcie - 25 days ago 6
Swift Question

Swift Firebase querying

I am trying to grab the uid given his email.

I have this:

let usersRef = Database.database().reference().child("users")

usersRef.queryOrdered(byChild: "email").queryStarting(atValue: email).queryEnding(atValue: email).observeSingleEvent(of: .value, with: { (snapshot) in
if let values = snapshot.value as? [String: Any]{
print(values)
}
})


And my database looks as follows:

users

|
-$uid

|
--email: "some email"

The snapshot doesn't return anything.

Also, the debugger tells me this:


Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "email" at /users to your security rules for better performance


I think I have done this like so:

{
"rules": {
"users": {
".read": "auth != null",
"$uid": {
".write": "$uid === auth.uid",
".indexOn": ["email"]
}


But this doesn't work as well.

Answer Source

You have to define Firebase Database indexes in the location where you execute the query. So that is one level higher than where you currently have it:

{
  "rules": {
    "users": {
      ".indexOn": ["email"],
      ".read": "auth != null",
      "$uid": {
        ".write": "$uid === auth.uid",
      }
    }
  }
}