for hours I`ve been trying and struggling to get rest of user attributes to get printed based on using the UID of the user. Here is how the "userRegistration" table looks like in my firebase db:
UserRegistration Table on firebase
Here is a sample code I tried which actually works perfectly when trying to access values from other tables, but does not work when trying to access data from "UserRegistration" table:
public void getRestOfTheUserAttributes(){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String the_uid = user.getUid();
DatabaseReference additionalUserInfoRef = rootRef.child("UserRegistration");
Query userQuery = additionalUserInfoRef.orderByChild("user_id").equalTo(the_uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Map<String, Object> map = new HashMap<>();
String userName = ds.child("username").getValue(String.class);
String userMail = ds.child("e-mail").getValue(String.class);
String nameOfUser = ds.child("name").getValue(String.class);
}
}
Again: this code above works perfectly if i try access data from other tables, but not on the "UserRegistration" table!
Also, I have created a very simple method to try get the rest of values but does not work either..
public void getUserProfile(){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String the_id = user.getUid(); //works perfectly
String name = user.getDisplayName(); // returns null
String email = user.getEmail(); //works perfectly!
}
Could someone plz help me?
thanks.
Try the following:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String the_uid = user.getUid();
DatabaseReference additionalUserInfoRef = rootRef.child("UserRegistraion");
Query userQuery = additionalUserInfoRef.orderByKey().equalTo(the_uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userName = ds.child("username").getValue(String.class);
String userMail = ds.child("e-mail").getValue(String.class);
String nameOfUser = ds.child("name").getValue(String.class);
}
}
First add a reference to node UserRegistraion
, then to retrieve data according the uid, you need to use orderByKey()
and check if it is equal to the id of the current user. Then you can iterate and retrieve the data
iDs are statistically unique. So you don't need to use for loop to iterate from user data. Just provide the user_id in child you can retrieve that user data directly
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String the_uid = user.getUid();
DatabaseReference additionalUserInfoRef = rootRef.child("UserRegistraion").child(the_uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String userName = dataSnapshot.child("username").getValue(String.class);
String userMail = dataSnapshot.child("e-mail").getValue(String.class);
String nameOfUser = dataSnapshot.child("name").getValue(String.class);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.