访问onAuthStateChanged中的实时数据库

时间:2018-04-28 11:41:43

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

我在使用Auth创建用户后尝试访问firebase数据库但没有成功。 使用Auth创建用户后,我想将其保存在实时数据库中并添加更多字段。

signUpBrother(email: string, password: string) {
  this.fireAuth.auth.createUserWithEmailAndPassword(email, password)

  this.fireAuth.auth.onAuthStateChanged(function(user) {
    this.firebase.list('brothers').set(uid, {
      'email': user.email,
      'nickname': 'nickname',
      'avatar': 'avatar',
      'is_big_brother': true
    })
  })
}

我得到的错误是:

  

错误错误:未捕获(在承诺中):TypeError:无法读取未定义的属性“firebase”

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

由于codtex评论this在回调中具有不同于其外部的含义。新this没有firebase属性。

为了确保您可以访问您的功能,您可以使用其他名称捕获this

signUpBrother(email: string, password: string) {
  this.fireAuth.auth.createUserWithEmailAndPassword(email, password)

  var that = this;    
  this.fireAuth.auth.onAuthStateChanged(function(user) {
    that.firebase.list('brothers').set(uid, {
      'email': user.email,
      'nickname': 'nickname',
      'avatar': 'avatar',
      'is_big_brother': true
    })
  })
}
相关问题