【问题标题】:failed to insert data to database after creating a user in authenication在身份验证中创建用户后无法将数据插入数据库
【发布时间】:2018-05-06 18:35:36
【问题描述】:

我想创建一个能够在 firebase 中进行身份验证的用户,然后将用户的数据插入数据库中。 我使用的代码如下:

this.app.auth().createUserWithEmailAndPassword(userForm.value.email, userForm.value.password)
        .then(res => {
            console.log('2 form data:', userForm.value);
            this._firebase.app.database().ref('users').push({
                email: userForm.value.email,
                uid: res.uid
            })
        })
        .catch(err => {
            console.log('Something went wrong:', err.message);
        });

我使用app 将用户插入authentication_firebase 将用户的数据插入databasecreateUserWithEmailAndPassword 有效,但出现以下错误:

Reference.push failed: first argument contains undefined in property 'users.email'

userForm 包含用户数据。有什么问题?

【问题讨论】:

  • 如果在 this._firebase.app.database().ref 之前执行 console.log({ email: userForm.value.email, uid: res.uid }) 在控制台中会看到什么('users').push ??
  • 我看到这个: Object { email: undefined, uid: "hguE4Qnh0VN0NI4NWlDr188A8Xt2" } 电子邮件未定义,因此所有表单数据都未定义。有什么解决办法吗?
  • 您的电子邮件值未定义,因此您收到错误消息。您应该从表单中获得正确的值。您可能会遵循 Muiz Mahdy 的回答。 (我没有 Angular 的资格,很抱歉我帮不上忙)
  • 似乎在 .then({}) 中所有的表单数据都是未定义的。这怎么可能?表单中给出的电子邮件和密码被插入到身份验证中
  • 你试过 Muiz Mahdy 的回答吗?

标签: angular firebase firebase-realtime-database


【解决方案1】:

按如下操作:

this.app.auth().createUserWithEmailAndPassword(userForm.value.email, userForm.value.password)
    .then(res => {
        console.log('2 form data:', userForm.value);

        var user = firebase.auth().currentUser;

        console.log(user.email);
        console.log(user.uid);

        return this._firebase.app.database().ref('users').push({
            email: user.email,
            uid: user.uid
        })
    })
    .catch(err => {
        console.log('Something went wrong:', err.message);
    });

在then中没有得到传递给函数的参数是正常的。异步函数createUserWithEmailAndPassword“返回一个包含非空firebase.auth.UserCredentialfirebase.Promise”见https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword

所以你必须在 then 函数中获取当前用户,firebase.auth().currentUser;


根据您的评论进行编辑(edit2:已删除:-))

做一个这样的函数,并用userForm的数据调用它:

function pushUser(userForm.value) {

    //create a JavaScript object holding the values you want to push to Firebase

    const objectToPush = {
        email: userForm.value.email,
        uid: userForm.value.uid,
        address: userForm.value.address,
        .....
    };

    return this.app.auth().createUserWithEmailAndPassword(userForm.value.email, userForm.value.password)
    .then(res => {

       //var user = firebase.auth().currentUser;  <- you don't need that anymore 
       //since you have the values in the object

        return this._firebase.app.database().ref('users').push(objectToPush);
    })
    .catch(err => {
        console.log('Something went wrong:', err.message);
    });

}

PS:注意this 仍然是你想要的。

【讨论】:

  • 这成功了!!!非常感谢您的帮助!!!我还添加了从 res 中获取的 uid: .then(res => { objectToPush.uid = res.uid; return this._firebaseAuth.app.database().ref('users').push(objectToPush); })
  • @Elias 很高兴我能帮助你。您可以通过单击答案左侧的灰色复选标记来接受答案,请参阅stackoverflow.com/help/someone-answers。谢谢
  • 你能检查一下吗? stackoverflow.com/questions/50610498/…
【解决方案2】:

我无法发表评论,所以我将把我的答案放在这里,

this._firebase.app.database().ref('users').push

this 不是指 firebase,因此您需要在函数 vat 的开头定义 = this,然后将您的代码更改为 this:

that._firebase.app.database().ref('users').push

【讨论】:

  • 更正的部分是这样的:return this.app.auth().createUserWithEmailAndPassword(userForm.value.email, userForm.value.password) .then(res =&gt; { objectToPush.uid = res.uid; return this._firebaseAuth.app.database().ref('users').push(objectToPush); }) .catch(err =&gt; { console.log('Something went wrong:', err.message); });你可以在上面看到雷诺的回答
【解决方案3】:

问题似乎出在“userForm.value.email”中。

获取表单输入的最简单方法是对 ngModel 使用双向绑定。

如果您使用的是响应式表单,请使用:

emailVal:string = this.formGroup.get('emailCtrl').value;

获取表单组(formGroup)的表单控件(emailCtrl)的值

【讨论】:

  • 我没有使用响应式表单,但是 this.app.auth().createUserWithEmailAndPassword(userForm.value.email, userForm.value.password) 有效,电子邮件和密码被插入到身份验证中。这些值取自表格。他们怎么能在这下面两行未定义?
猜你喜欢
  • 2020-09-12
  • 2021-06-11
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2018-03-09
  • 1970-01-01
  • 2019-02-01
相关资源
最近更新 更多