2016-10-17 66 views
1

我用下面的登录方法数据库:Angularfire2新用户添加到

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相同的结构添加新的用户对象到我的数据库?以及为什么我不能运行我的代码,如果它与记录相同?

更新

我的新代码:

this.af.database.object('/users/${this.authService.userData.uid}').set({ 
       provider: this.authService.userData.provider, 
       name: this.getName(this.authService.userData) 
      }); 
  • 没有用户插入(我的火力数据库是空的)
+0

https://github.com/angular/angularfire2/blob/master/docs/2-retrieving-data-as-objects.md#api-summary – cartant

回答

4

使用object();孩子()不存在:

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

爽一把,谢谢。但你忘了在'.af'之后添加'.database'。无论如何,我不知道为什么,但没有添加到我的数据库。更新我的代码 – TheUnreal

+0

您是否收到错误?你的安全规则好吗? – Sasxa

+0

我没有得到任何错误。我rulese是:'{ “规则”:{ “.read ”: “真”, “ .WRITE”: “真正的” } }'在我火力数据库控制台我只看到'null' – TheUnreal

0

这是一个较短的版本。

import { AngularFireDatabase } from "angularfire2"; 

... 

constructor(private afd: AngularFireDatabase) {} 

... 

this.afd.object(`users/${this.authService.userData.uid}`).set({...});