1
votes

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.

2
What is the expected output? please explain what is the purpose of query?3stud1ant3
Question updated.Latcie
Try this : usersRef.queryOrdered(byChild: "email").queryEqual(toValue:email).observeSingleEvent3stud1ant3
It's hard to be certain what's happening without seeing the exact JSON. Can you get this by clicking the "Export JSON" link in your Firebase Database console and add it to your question?Frank van Puffelen
Thanks, 3stud1ant3, that worked.Latcie

2 Answers

1
votes

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",
      }
    }
  }
}
0
votes

change like below :

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