This is my database
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.
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.
Using set() overwrites data at the specified location, including any child nodes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With