Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't get firebase userID

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.

enter image description here

like image 770
Ola Avatar asked Sep 10 '26 13:09

Ola


1 Answers

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);
})
like image 89
Frank van Puffelen Avatar answered Sep 12 '26 02:09

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!