Retrieve Current User's Data (user_id) Firebase?

前端 未结 3 1056
星月不相逢
星月不相逢 2020-12-22 03:07

        firebaseAuth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance();
        mDb = mDatabase.getReference();
        Fireb         


        
相关标签:
3条回答
  • 2020-12-22 03:47

    To solve this, please use the following code:

    firebaseAuth = FirebaseAuth.getInstance();
    mDatabase = FirebaseDatabase.getInstance();
    mDb = mDatabase.getReference();
    FirebaseUser user = firebaseAuth.getCurrentUser();
    userKey = user.getUid();
    
    mDb.child("Tunanetra").child(userKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String userID = dataSnapshot.child("user_id").getValue(String.class);
            Log.d(TAG, "Name: " + userID);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
    

    With this code, you'll be able to print out in the logcat, the id of the users that is authenticated and exists under Tunanetra node. If the user exists under the Orangtua node, then just change Tunanetra with Orangtua.

    0 讨论(0)
  • 2020-12-22 03:50

    It's simple. If you want to retrieve user data. Then you have to first select the child node in which you want to look for the user.

    You have to Nodes Tunanetra and Orangtua

    Else you can try this order by approach.

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
            FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
            DatabaseReference mDb = mDatabase.getReference();
            FirebaseUser user = firebaseAuth.getCurrentUser();
            String userKey = user.getUid();
    
            //First Approach
            mDb.child("Tunanetra").child(userKey).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String userID = String.valueOf(dataSnapshot.child("user_id").getValue());
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
    
            //Second Approach
            mDb.child("Tunanetra").orderByChild("email").equalTo(user.getEmail()).limitToFirst(1).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
    
                    if(dataSnapshot.getChildrenCount()>0){
                        for(DataSnapshot data : dataSnapshot.getChildren()){
                            String userID = String.valueOf(data.child("user_id").getValue());
                        }
                    }
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
    
    0 讨论(0)
  • 2020-12-22 04:10

    I think instead of

    mDb = mDatabase.getReference(); 
    

    you should be using

    mDb = mDatabase.getReference("Tunanetra");
    

    because that's where the user you're looking for is nested at.

    0 讨论(0)
提交回复
热议问题