首页 文章

Angularfire2将新用户添加到数据库中

提问于
浏览
1

我使用了以下登录方法:

login() {
  this.auth.login();
this.authService.login().subscribe(() => {
  if (this.authService.isLoggedIn) {
      console.log("ADDING THE USER..");
      // Add a new user to the users table
      this.af.child("users").child(this.authService.userData.uid).set({
          provider: this.authService.userData.provider,
            name: this.getName(this.authService.userData)
      });
    // Redirect the user
    this.router.navigate(['/panel']);
  }
});

}

其中几乎与文档中的方法相同:https://www.firebase.com/docs/web/guide/user-auth.html#section-storing

当我运行我的应用程序时,我收到以下错误:

Property 'child' does not exist on type 'AngularFire'.

并且未添加用户 . 怎么了?

我只需要在Firebase数据库中添加一个新对象 . 以下工作,但我需要如上所示的结构:

this.users = this.af.database.list('/users');
this.users.push("TEST");

那么,如何使用https://www.firebase.com/docs/web/guide/user-auth.html#section-storing中的相同结构将新用户对象添加到我的数据库中?为什么我可以和记录在一起_2882757?

Update

我的新代码:

this.af.database.object('/users/${this.authService.userData.uid}').set({
              provider: this.authService.userData.provider,
                name: this.getName(this.authService.userData)
          });
  • 未插入用户(我的firebase数据库为空)

2 回答

  • 0

    这是一个较短的版本 .

    import { AngularFireDatabase } from "angularfire2";
    
    ...
    
    constructor(private afd: AngularFireDatabase) {}
    
    ...
    
    this.afd.object(`users/${this.authService.userData.uid}`).set({...});
    
  • 4

    使用 object() ; child()不存在:

    this.af.database.object(`users/${this.authService.userData.uid}`).set({...})
    

相关问题