How can I retrieve values from Firebase database based on email entered by user in text field?

后端 未结 2 1649
小鲜肉
小鲜肉 2021-01-26 16:19

I am working on Firebase database. How can I retrieve values from database based on email entered by user in text field. Below is my database structure. Kindly help.

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-26 17:03

    You have:

     Registration
           UserRegistration
                   12
                    pushid
                      key:values
                      email:emailvalue
    

    To retrieve values always you have to go from top to bottom, so you cannot skip any node.

    Only orderByChild(..) query can skip one node (which means you use this query if you want to get children that are not direct children)

    Try this:

    DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Registration").child("UserRegistration").child("12");
    ref.orderByChild("email").equalTo(emailenteredbyuser).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         for (DataSnapshot datas : dataSnapshot.getChildren()) { 
               String email=datas.child("email").getValue().toString();
    
              }
            }
            @Override
        public void onCancelled(FirebaseError firebaseError) {
    
           }
    
     });
    

    As you can see in the above, the reference passes through every node until node 12.

    Then you use this orderByChild("email").equalTo(emailenteredbyuser) to get the email entered by the user.

    for (DataSnapshot datas : dataSnapshot.getChildren()) this for loop the datas iterates in inside the direct children of 12 which is the pushid that way you do not need to retrieve the pushid to get the values inside of it.

提交回复
热议问题