3

I'm trying to check if a username exists or not. When I call queryOrderedBychild, snapshot value is always available but it will print down the whole database of mine, not just the data which I request queryOrderby. When I call queryEqualToValue, it always return null. I've tried many ways to figure it out.

This is my code:

DataService.instance.UsersRef.queryOrdered(byChild:"Nickname").queryEqual(toValue: "kai1004pro").observe(.value, with: { (snapshot) in

        if (snapshot.value is NSNull) {
            print("not found")
        } else {
            print("found")
            print(snapshot.value)
        }




    })

This is my json tree:

Optional({
g50X0FvEsSO4L457v7NzS3dAABl1 =     {
    "User Profile" =         {
        Birthday = "Sep 20, 1992";
        Gender = Female;
        Nickname = kai1004pro;
        UserUID = g50X0FvEsSO4L457v7NzS3dAABl1;
        emailAddress = "poqksbs@gmail.con";
        isFollow = 0;
        isFriend = 0;
    };
};
})

This is the security rules :

"rules": {
".read": true,
".write": "auth != null",
"Users": {
  ".read": true,
  ".write": "auth != null",
  ".indexOn": ["Nickname", "User Profile"]
    }
  }
}
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • What is your UsersRef? My guess is that it links to the users id (g50X0FvEsSO4L457v7NzS3dAABl1) in this case, and Nickname is not a direct child of that, so its never finding it because its not searching the right place. When I was going through this, I also remember having an issue with .value when searching through child nodes, I used .ChildAdded and .ChildChanged instead I believe. – Dallas Sep 21 '16 at 06:00

1 Answers1

4

Modify your JSON tree to include a separate node active_usernames, in that add username of every user whenever you create new user, modify whenever your user modify its username...

myApp:{
  users:{
    uid1:{....},
    uid2:{....},
    uid3:{....},
    uid4:{....},
  }
active_usernames :{
    uid1username : "true",
    uid2username : "true",
    uid3username : "true",
    uid4username : "true"
    }
}

To check your if username already exists:-

//Checking username existence
FIRDatabase.database().reference().child("active_usernames").child(self.enteredUsername.text!).observeSingleEvent(of: .value, with: {(usernameSnap) in

        if usernameSnap.exists(){
        //This username already exists

        }else{

        //Yippee!.. This can be my username
        }

    })

Also change your security rules to be readable (ONLY READABLE) to all, by manipulating the security rules.

{rules :{

   active_usernames : {

      ".read" : "true",
      ".write" : "auth != null"

      }
    }
   }

PS:- enteredUsername is the textField in which you enter your username.

Suggestion:- keep the checking code inside didChangeEditing event of the textField(For better user experience.!)

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Sorry that I was sleeping the whole day. I was very tired. I have many question about Firebase that I need to ask. Can't post all question here. May I have your contact by email or Facebook, anything that I can reach to you. Thank you very much !!! – Khôi V Nguyễn Sep 21 '16 at 21:21
  • 3
    @KhôiVNguyễn That's NOT how stackoverflow works. If you have good questions then bring them here.write your questions at a time where most users are online, that's usually 11am-4pm EST. And hopefully you will get an answer. Or possibly find the answer to your questions in previous answers. – mfaani Nov 19 '16 at 11:54