Firebase authentication for checking if the user exists in the database or not

元气小坏坏 提交于 2020-01-05 05:52:08

问题


This is my Firebase database structure

STUDENT
 ur14cs002
    id:ur14cs002
    pass:abc
 ur14cs001
    id:ur13cs001
    pass:def

I have to check with my user's id like when user enter in edit text that needs to be check if that particular user exists in the database or not. Below code this is what i did bt this code is not working at all.

  EditText id = (EditText) findViewById(R.id.userEmail);

    EditText password = (EditText) findViewById(R.id.userPassword);
    database = FirebaseDatabase.getInstance();

    String child = id.getText().toString();
    String pass = password.getText().toString();
    myRef = database.getReference("STUDENT").child(child);
    myRef.orderByChild("id").equalTo(child).addValueEventListener(new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
            if(dataSnapshot.exists() ){
                Toast.makeText(getApplicationContext(),"PRESENT",Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });}}

回答1:


Your reference is wrong:

 myRef = database.getReference("STUDENT").child(child);

In the above the dataSnapshot will be the child which is this id:ur14cs002 for example. So ofcourse it will never exists, since the dataSnapshot will be the child (which is the id provided).

You need to do this:-

  myRef = database.getReference().child("STUDENT");

This way the reference is at node STUDENT then orderByChild("id").equalTo(child) will give you the right result. Here the dataSnapshot will be the (STUDENT) node.



来源:https://stackoverflow.com/questions/48500719/firebase-authentication-for-checking-if-the-user-exists-in-the-database-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!