Firebase身份验证,用于检查用户是否存在于数据库中 [英] Firebase authentication for checking if the user exists in the database or not

查看:77
本文介绍了Firebase身份验证,用于检查用户是否存在于数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Firebase数据库结构

This is my Firebase database structure

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

我必须使用用户的ID进行检查,就像用户输入需要检查该特定用户是否存在于数据库中的编辑文本时一样. 在下面的代码中,这是我所做的工作,但是此代码根本无法工作.

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) {

        }


    });}}

推荐答案

您的引用有误:

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

在上面的数据快照中将是child,例如id:ur14cs002.因此,它当然将永远不存在,因为dataSnapshot将是子代(提供的ID).

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");

这样,引用将在节点STUDENT处,然后orderByChild("id").equalTo(child)将为您提供正确的结果.这里的dataSnapshot将是(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.

这篇关于Firebase身份验证,用于检查用户是否存在于数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆