Raj Kumar
Raj Kumar

Reputation: 796

FireBase Rules indexOn Issue

I am trying to get all emails of users using firebase Query like below

private void searchUserByEmail(final String searchText) {  
        DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
        Query query = mFirebaseDatabaseReference.child(Constants.FB_TABLE_USERS).orderByChild(Constants.FB_EMAIL).equalTo(searchText);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
}}

And While Searching Any email Which is not in list I am getting a warning message in my console Like

W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "email"' at table_users to your security and Firebase Database rules for better performance

And My Rules in FireBase is

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

This is my Users Table screenshot enter image description here

Upvotes: 1

Views: 516

Answers (1)

Ali Faris
Ali Faris

Reputation: 18592

you should remove the brackets from ".indexOn" value

"table_users": {
  ".indexOn": "email"
}

query.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String email = snapshot.child("email").getValue();
            System.out.print(email);
        }
    }
});

Upvotes: 2

Related Questions