Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Retrieve data of a specific user from a Firebase database

I am trying to read "specific" data from my firebase realtime database. I have seen examples of getting all data from a table and going through it to find the data record you want, but that is definitly not a recommended safe practice.

Below is how I save my data

private void saveInDatabase(String email)
    {
        // Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("user");
        String key = myRef.push().getKey();

        User user = new User();
        user.setCountry("United States");
        user.setEmail(email);
        user.setFirstName("John");
        user.setLastName("Doh");
        user.setGender("Male");

        myRef.child(key).setValue(user);

    }

Well that works great. But now I need to retrieve the data record from the user table, that is only belong to John. If I do this with MySQL, it will be smething like select * from user where primaryKey =JOHN_PRYMARY_KEY

I gave it a try, check below.

database.getReference("user" + FirebaseAuth.getInstance().getCurrentUser().getUid());

I am really not sure how I can proceed from here, I see no method to go forward. Please advice.

UPDATE

I tried the answers from peter haddad and others. When I try to get the data from DataSnapshot, it throws a NullPointerException. The OnDataChanged is getting fired. Below is my database.

enter image description here

I tried to get data in 3 different ways, check below.

----No 1----

private void getUserData()
    {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user");

        reference.orderByChild("email").equalTo(FirebaseAuth.getInstance().getCurrentUser().getEmail()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot datas: dataSnapshot.getChildren()){
                    String familyname=datas.child("familyName").getValue().toString();
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

----No 2----

private void getUserData()
{
    FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
    String userid=user.getUid();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user");
    reference.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String email = dataSnapshot.getValue(User.class).getEmail();
            String firstName = dataSnapshot.getValue(User.class).getFirstName();
            Log.d("Datasnapshot",email+" "+firstName);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

----No 3----

private void getUserData() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("user");

    Query specific_user = myRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    specific_user.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //here you will get the data
                    String email = dataSnapshot.getValue(User.class).getEmail();
                    String firstName = dataSnapshot.getValue(User.class).getFirstName();
                    Log.d("Datasnapshot", email + " " + firstName);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {



                }
            });
}

This is the error I get. Based on the codeset I use, the place where the Null pointer trigger get different, the normal behavior.

rebaseauth D/NetworkSecurityConfig: No Network Security Config specified, using platform default
04-25 11:49:46.340 31202-31271/com.example.yohan.firebaseauth V/FA: Inactivity, disconnecting from the service
04-25 11:49:47.793 31202-32166/com.example.yohan.firebaseauth W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "email"' at user to your security and Firebase Database rules for better performance
04-25 11:49:47.794 31202-31202/com.example.yohan.firebaseauth D/AndroidRuntime: Shutting down VM
04-25 11:49:47.794 31202-31202/com.example.yohan.firebaseauth E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.yohan.firebaseauth, PID: 31202
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                                    at com.example.yohan.firebaseauth.MainActivity$6.onDataChange(MainActivity.java:249)
                                                                                    at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                                    at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
                                                                                    at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                                    at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
like image 262
PeakGen Avatar asked Apr 24 '18 10:04

PeakGen


1 Answers

You need to do the query:

orderByChild("FirstName").equalTo(name);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user");

reference.orderByChild("firstName").equalTo(name).addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String familyname=datas.child("familyName").getValue().toString();
    }
 }
   @Override
public void onCancelled(DatabaseError databaseError) {
  }
 });

Assuming you have this database:

user
  randomid
     firstName: John
     familyName: familyName_here

Another way is to use the userid to be able to retrieve the data, but as I see in the question you are saving a random id using push().

You need to retrieve the userid:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();

then add it to the database:

 myRef.child(userid).setValue(user);

then to retrieve data only for John:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user");
reference.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {..}
like image 158
Peter Haddad Avatar answered Nov 02 '22 23:11

Peter Haddad