When creating a user with email, how to add elements in DB in Firebase in Javascript

时间:2018-06-04 16:41:44

标签: javascript firebase firebase-authentication

So i'm new to using Firebase and I have been looking at the Firebase documentation but I haven't found how to do the following:

The user puts the following inputs: username, email, password, confirm password. When I call the "createUserWithEmailAndPassword", if it works, I want at the same time to create entries in the DB in Firebase. I tried writing the following line:

firebase.auth().createUserWithEmailAndPassword(userEmailCreate, userPasswordCreate).then(function(){
  //do what I want
}).catch(function (error) {})

But it doesn't work. What am I doing wrong? Or do you have any suggestions as to how to add additional elements in the entry User using the uid that is created by the authentication method?

    function createUser() {
  //instance of database Firebase
  var database = firebase.database();
  //Get inputs from user
  var userUsername = document.getElementById("usernameCreation_Field").value;
  var userEmailCreate = document.getElementById("emailCreation_field").value;
  var userPasswordCreate = document.getElementById("passwordCreation_field").value;
  var userPassword2Create = document.getElementById("passwordCreation2_field").value;

  if (userPasswordCreate == userPassword2Create && userUsername.length > 0) {
    //If passwords match try to create user
    firebase.auth().createUserWithEmailAndPassword(userEmailCreate, userPasswordCreate).catch(function (error) {
      // Handle Errors here when creating account
      var errorCode = error.code;
      var errorMessage = error.message;
      document.getElementById('createSuccess').style.display = 'block';
      document.getElementById('createSuccess').innerHTML = errorMessage;
    });
    //Passwords don't match
  } else if(userUsername.length == 0){
    document.getElementById('createSuccess').innerHTML = 'Username must be filled!';
    document.getElementById('createSuccess').style.display = 'block';
  } else {
    document.getElementById('createSuccess').style.display = 'block';
    document.getElementById('createSuccess').innerHTML = 'Passwords don\'t match!';
  }

}

//Add new user to database
function setUserData(userId, username, email){
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl,
    topScore : topScore
  });
}

Thank you in advance for your time!

1 个答案:

答案 0 :(得分:0)

Your code is not calling setUserData anywhere, so that would definitely explain why nothing is written to the database.

You're probably looking to call if from the then of createUser...:

firebase.auth().createUserWithEmailAndPassword(userEmailCreate, userPasswordCreate).then(function(userCredential) {
  setUserdata(userCredential.user.uid, userUsername, userEmailCreate);
}).catch(function (error) {
  // Handle Errors here when creating account
  var errorCode = error.code;
  var errorMessage = error.message;
  document.getElementById('createSuccess').style.display = 'block';
  document.getElementById('createSuccess').innerHTML = errorMessage;
});