如何检查firebase数据库中是否存在值 - android

时间:2020-12-27 04:28:44

I am new to firebase via Android(Java) and was wondering how to check if a user already exists in a database of the following format:

我是Android(Java)的firebase新手,想知道如何检查用户是否已存在于以下格式的数据库中:

    gdb-2fbgg{

      users{


         Michael{
              age: 20
              height: 150
              name: Michael
                }


         Bob{
              age: 20
              height: 150
              name: Bob
                }
       }
}

For example upon a button press, I would like to check the firebase to see if the username "Michael" exists ( first level michael - not "michael" within object michael.

例如按下按钮,我想查看firebase以查看用户名“Michael”是否存在(第一级迈克尔 - 而不是对象michael中的“michael”。

Thank you

谢谢

1 个解决方案

#1


5  

Use .child("Michael").exists() method or .hasChild("Michael") with SingleValueEvent within onDataChange

在onDataChange中使用带有SingleValueEvent的.child(“Michael”)。exists()方法或.hasChild(“Michael”)

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference users = root.child("users");
users.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.child("Michael").exists()) {
                // run some code
            }else{

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

#1


5  

Use .child("Michael").exists() method or .hasChild("Michael") with SingleValueEvent within onDataChange

在onDataChange中使用带有SingleValueEvent的.child(“Michael”)。exists()方法或.hasChild(“Michael”)

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference users = root.child("users");
users.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.child("Michael").exists()) {
                // run some code
            }else{

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });