Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - generate child with children for a registered user

I want to add in Firebase multiple data when a user registers to my app, including a node "Scores" with some children, but I don't know how to add it.

Firebase Database:

"Users" : {
   "L89LA3099j-VAhi5Y5P" : {
      "Admin" : false,
      "Email" : "[email protected]",
      "Nickname" : "Anonymous",
      "Picture" : "https://firebasestorage.googleapis.com/v0/b/cul...",
      "Scores" : {
         "Arts" : 0,
         "Classical music" : 0,
         "Literature" : 0
      }
   }
}

And here is the code in Android Studio:

 FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password).addOnCompleteListener(UserRegistration.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (!task.isSuccessful()) {
                        Toast.makeText(UserRegistration.this, "Invalid email or already existed email!", Toast.LENGTH_SHORT).show();
                    } else {
                        DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
                        HashMap<String, Object> user_data = new HashMap<>();
                        user_data.put("Admin", false);
                        user_data.put("Email", email); // I got it correctly from EditText
                        user_data.put("Picture", "https://firebasestorage.googleapis.com/v0/b/cul...");
                        user_data.put("Nickname", "Anonymous");
                        usersRef.child("Users").push().setValue(user_data);

                        DatabaseReference scores_ref = usersRef.child("Scores"); // I guess it's wrong to do it like this
                        HashMap<String, Integer> user_scores = new HashMap<>();
                        user_scores.put("Arts", 0);
                        user_scores.put("Classical music", 0);
                        user_scores.put("Literature", 0);

                        // and now how to add Scores node inside the created user?
like image 552
Robert Pal Avatar asked Mar 21 '18 16:03

Robert Pal


1 Answers

If you want to add Scores under a user then try the following:

First use the userid and not the push()

FirebaseUser mCurrentUser= task.getResult().getUser();
String userid=mCurrentUser.getUid();

then change this:

usersRef.child("Users").push().setValue(user_data);

to this:

usersRef.child(userid).setValue(user_data);
DatabaseReference scores_ref =usersRef.child(userid).child("Scores"); 
scores_ref.child("Arts").setValue(0);
scores_ref.child("Classical music").setValue(0);
scores_ref.child("Literature").setValue(0);

userRef points to child Users

this line usersRef.child(userid).setValue(user_data);

will create the following:

Users
  userid
     Admin : false
     Email : [email protected]
     Nickname" : Anonymous
     Picture" : https://firebasestorage.googleapis.com/v0/b/cul...

Then this line DatabaseReference scores_ref =usersRef.child(userid).child("Scores");

will pass through node Users, then node userid and it will add child Scores and it's children. All will be under the current userid.

like image 111
Peter Haddad Avatar answered Oct 20 '22 04:10

Peter Haddad