Firebase使用电子邮件和密码创建用户并将数据放入数据库

时间:2017-12-11 13:14:40

标签: javascript firebase-realtime-database firebase-authentication

我想用createUserWithEmailAndPassword函数创建用户,然后将该用户的数据放入我的数据库,但它不起作用..用户被添加到firebase中的身份验证选项卡,但不会添加到我的数据库中。我也没有收到错误。

registerUser.addEventListener('click', function (user) {
    event.preventDefault();
    closeRegisterForm();

    email = registerEmail.value;
    password = registerPassword.value;

    firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(function (event) {
            var ref = firebase.database().ref("users").child(user.uid).set({
                email: user.email,
                uid: user.uid
            });
        })
        .catch(function (error) {
            var errorCode = error.code;
            var errorMessage = error.message;
        });
});

3 个答案:

答案 0 :(得分:1)

嗨,我遇到了完全相同的问题。但是我用一个局部函数来解决它。像这样:

createNewUser(form) {
//create new user with provided data of the form
this.afAuth.auth.createUserWithEmailAndPassword(form.email, form.password)
  .then(function(firebaseUser) {
    console.log("User " + firebaseUser.uid + " created successfully!");
    updateFirestore(form, firebaseUser.uid);
    return firebaseUser;
  }).catch(function(error) {
    alert(error)
  });

function updateFirestore(form, uidNewUser) {
  //push data into firestore using the uid provided

  let data = {};
  data['mail'] = form.email;
  data['name'] = form.name;
  //se empuja el arreglo data en el documento del usuario
  this.afs.collection('users').doc(uidNewUser).set(data);
  console.log(data, uidNewUser);
}

}

答案 1 :(得分:0)

您引用user.uiduser.email但从未定义user。方法createUserWithEmailAndPassword中的返回登录类型是用户,但您将其定义为event。另外,请确保等待db write promise解决并捕获任何错误,如Frank建议的那样。

答案 2 :(得分:0)

为我工作:

rowcol = input("Column or row?")
if rowcol == "column":
  column = int(input("Which column?"))
  result = data.iloc([column])
elif rowcol == "row":
  row = int(input("Which row?"))
  result = data.iloc[[row]]
else:
  print("Invalid response.")

print(result)