Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding if data exists in Firebase

I am using the following to fetch a username from Firebase. But in this case, how do I get to know, if the username exists or not.

Firebase firebaseRef = new Firebase("<Firebasae_URL>/Users");
Query query=firebaseRef.orderByChild("username").equalTo("username");
like image 860
Arun George Avatar asked Apr 24 '16 17:04

Arun George


1 Answers

So in your case the code should be like this, to find an username exists in your database.

Firebase firebaseRef = new Firebase("<Firebasae_URL>/Users").child("username");
firebaseRef.addListenerForSingleValueEvent(new ValueEventListener) {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // User Exists
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});
like image 67
Reaz Murshed Avatar answered Sep 22 '22 12:09

Reaz Murshed