【问题标题】:Firebase create user with email and password and put data in databaseFirebase 使用电子邮件和密码创建用户并将数据放入数据库
【发布时间】:2017-12-11 13:14:40
【问题描述】:

我想使用函数 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;
        });
});

【问题讨论】:

  • 哦,太方便了,你没有提供错误信息;)
  • @entio 没有错误消息,用户被添加到我在 firebase 的身份验证选项卡中,但不在我的数据库中
  • 写入数据库时​​您没有处理潜在问题。你要么需要返回承诺,要么抓住它:firebase.database().ref("users").child(user.uid).set({ email: user.email, uid: user.uid }).catch(function(error) { console.error(error); });
  • 这真是个奇怪的错误。它现在甚至存在!

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


【解决方案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);
  }
}

【讨论】:

  • 实际上不工作。为什么会存在这个错误?
【解决方案2】:

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

【讨论】:

  • 嘿@bojeil 我在确认验证码后使用电话号码登录我调用 createUserWithEmailAndPassword() 但结果是两个不同的用户!我怎样才能让它从“signInWithPhone 号码”中引用 uid?
  • 我不确定你到底要做什么。似乎与这个问题无关。为它打开一个新问题。在你signInWithPhoneNumber之后。您可以使用user.linkWithCredential 链接电子邮件/密码凭据。调用createUserWithEmailAndPassword 将创建一个不同的用户。
【解决方案3】:

为我工作:

        const val = this.signupForm.value;
        this.authService.doRegister(val)
            .then(res => {
                this.msg = 'You have registered successfully!';
                this.msgType = 'success';
                this.authService.updateUser(val.name, null)
                    .then(suc => {
                        const created = firebase.firestore.FieldValue.serverTimestamp();
                        const user = {
                            first_name: val.name,
                            last_name: '',
                            email_personal: val.email,
                            created: created,
                            updated: created
                        };
                        this.userCollection = this.afs.collection<User>('users');
                        this.userCollection.doc(suc).set(user);
                    }, err => {
                        // console.log(err);
                    });
                this.router.navigate(['/']);
            }, err => {
                this.msg = err.message;
            })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 2021-08-04
    • 2016-11-27
    • 2020-10-08
    • 2019-06-18
    • 2019-07-10
    • 2022-01-15
    • 2016-12-13
    相关资源
    最近更新 更多