2016-10-17 67 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({...});