Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase add new child with specified name

This is my database

enter image description here

I'd like to add new users to database keeping that format. I've tried this:

   //Store data in database
    Firebase usersRef = ref.child("Users");
    Map<String, String> userData = new HashMap<String, String>();

    userData.put("Nombre", name);
    userData.put("Password", pass);
    userData.put("Confirmed", "FALSE");
    userData.put("Email", mail);

    usersRef.setValue(name);
    usersRef = ref.child("Users").child(name);
    usersRef.setValue(userData);

The problem is whenever I add a new user, the previous one is overwritten.

like image 293
Aldridge1991 Avatar asked May 04 '16 14:05

Aldridge1991


People also ask

What does getKey () do Firebase?

Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value. Using Firebase to generate unique keys in this way is also of particular use when an app has multiple users creating child nodes at the same path in the tree.

What does the set () method in the Firebase library do?

Using set() overwrites data at the specified location, including any child nodes.


1 Answers

It happens because you are using:

Firebase usersRef = ref.child("Users");
usersRef.setValue(name);

In this way you will push the value in url/Users removing the previous value.

Check the doc:

Using setValue() will overwrite the data at the specified location, including any child nodes.

To add a new user without removing the previous one , just remove this line:

//usersRef.setValue(name);

In this way you will push the value in url/Users/myName without overriding other values.

like image 170
Gabriele Mariotti Avatar answered Sep 20 '22 20:09

Gabriele Mariotti