I am a newbie to firebase and Javascript and I have tried many methods get the user data store by the userid but all i'm getting is the data is being stored under "undefined".
here is my JS code
function handleRegister() {
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var ref = firebase.database().ref();
console.log(email);
console.log(fname);
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(user) {
var uid = user.uid;
var postData = {
Firstname: fname,
Lastname: lname,
email: email,
};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('Users').push().uid;
var updates = {};
updates['/Users/' + newPostKey] = postData;
// updates['/user-posts/' + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
})
}
What I'm trying to do is that after a new user has been registered, I want to save the user info(i.e first and last name) into the database under their respective uid generated during authentication. but currently i can't get the uid and new user replaces previously entered user in DB instead of being entered as a new entry.

You're complicating things a bit, by combining parts from the samples.
Saving the user that has just registered takes this:
firebase.auth().createUserWithEmailAndPassword(email, password) .then(function(user) {
var root = firebase.database().ref();
var uid = user.uid;
var postData = {
Firstname: fname,
Lastname: lname,
email: email
};
root.child("Users").child(uid).set(postData);
})
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