How to check if a child exists in firebase? [dupli

2020-07-16 02:35发布

问题:

I have a firebase that has a node called users. The user gives a username and I want to check to see if that username already exists as a child of the users node. This is the code I am currently trying. The platform is android.

String myUsername = userNameInputET.getText().toString();
if(mUsersRef.child(myUsername) == null) {
//do stuff
} else {
//do other stuff
}

This code is not working, it always goes to the else, even if the child with the given username does not exist. What to do?

回答1:

    String myUsername="User";

    if (root.child(myUsername).getRoot() == null) {

    } else {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user", "");
        root.updateChildren(map);    


回答2:

hello check this https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot

// Test for the existence of certain keys within a DataSnapshot
var ref = firebase.database().ref("users/ada");
ref.once("value")
  .then(function(snapshot) {
    var a = snapshot.exists();  // true
    var b = snapshot.child("name").exists(); // true
    var c = snapshot.child("name/first").exists(); // true
    var d = snapshot.child("name/middle").exists(); // false
  });


回答3:

Wasted two hours on this problem. Tried everything but none of the solutions worked. I used data read code of firebase onChildAdded to get the user data then determine it using if else condition.

In onCreate

mFirebaseDatabase=FirebaseDatabase.getInstance();
mMessagesDatabaseReference=mFirebaseDatabase.getReference().child("user").child(root).child(id);
    attachDatabaseReadListener();

Listener method

    private void attachDatabaseReadListener() {
    if ( mChildEventListener == null) {
        mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                User u = dataSnapshot.getValue(User.class);
                mUser=u.GetUser();
                mEmail=u.GetEmailId();
                Toast.makeText(getApplicationContext(),"here"+mUser,Toast.LENGTH_SHORT).show();
            }
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
            public void onChildRemoved(DataSnapshot dataSnapshot) {}
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
            public void onCancelled(DatabaseError databaseError) {}
        };
        mMessagesDatabaseReference.addChildEventListener( mChildEventListener);

    }
}

Condition check, It can be anything.

 if(mUser==null&&mEmail==null) {
      //do something here
  }
 else
 {
 //do nothing
 }

Note: some time is required to execute on child added so add proper delay I have only one Node so this code worked perfectly for me. If any other solution or easier solution is available please let me know.

if root node doesn't exists then also it will show null so keep that in mind before deploying application push root node.



回答4:

I believe you need to change your code to

String myUsername = userNameInputET.getText().toString();
if(mUsersRef.child(myUsername).getValue() == null) {
   //do stuff
} else {
   //do other stuff
}