0
votes

I would like to check if a username exist.

But Xcode is complaining about my security rules.

[Firebase/Database][I-RDB034028] Using an unspecified index. Consider adding ".indexOn": "profile/username" at /users to your security rules for better performance

If I delete the "$uid" Xcode does not complains anymore but my script does not works better.

the snapshot is always null even if I enter a username present in firebase

I've checked already the forum and tried different solutions from different users but nothing fixed my problem, so maybe I am doing something wrong ?

This is my rules

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

And this is my script

//check if username exist in the database
    static func checkIfUsernameExist(username: String, callback: @escaping (_ found: Bool) -> Void){
        print(username)
        let databaseRef = Database.database().reference(withPath: "users")
        databaseRef.queryOrdered(byChild: "profile/username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
            callback(snapshot.exists())
        })
    }

enter image description here

1

1 Answers

1
votes

Based on my limited experience, i add .child after databaseRef

databaseRef.child("users").queryOrdered(byChild: "profile/username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { (snapshot) in
        print(snapshot)
        callback(snapshot.exists())
    })

As for .indexOn, it will help once your data grow in size.