0

Currently I am adding my user class to a firebase database using this code:

public void onClick(View v)
        {

            Firebase ref = new Firebase("https://xxxxxx.firebaseio.com/");

            createAccount(emailString, passwordString);

            User user = new User ();
            user.setEmail(emailString);
            user.setPassword(passwordString);

            ref.child("users").push().setValue(user);



        }

Right now, since I use the .push() method, I am creating a unique ID in my database. How do I pull that unique ID? I looked at this tutorial but I don't understand how to implement it.

Community
  • 1
  • 1
user955857
  • 191
  • 4
  • 14

2 Answers2

3
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); //get the reference to your database
User user = new User ();
user.setEmail(emailString);
user.setPassword(passwordString);
String yourKey = dbRef.child("users").push().getKey(); //get the key
dbRef.child("users").child(yourKey).setValue(user); //insert user in that node

But if you want to access that node (yourKey) later, you will need to store it in some sort of permanent storage like a database on your web server.

Dusan Jovanov
  • 547
  • 5
  • 21
2

Great example of how to get key check these docs out helped me a lot. Firebase Docs

// Get a key for a new Post. 

var newPostKey = firebase.database().ref().child('posts').push().key;
edumas000
  • 272
  • 1
  • 3
  • 10