2016-10-28 24 views
0

如何將一個Observable分配給FirebaseObjectObservable?Angularfire2 - 爲FirebaseObjectObservable分配一個Observable?

此刻,我碰到下面的錯誤回到

Type '{}' is not assignable to type 'FirebaseObjectObservable<UserProfile[]>'.                         
    Property '_ref' is missing in type '{}'. 

方法getUserProfile

getUserProfile() { 
    const getUser = new Subject(); 
    this.getUser().subscribe(
     authResp => { 
      console.log('response received from getUser():', authResp); 
      const data = this.af.database.object(`users/${authResp}`); 
      getUser.next(data); 
      getUser.complete(); 
     }, 
     err => { 
      getUser.error(err); 
      getUser.complete(); 
     } 
    ) 
    return getUser.asObservable(); 
    } 

組件

export class HomeComponent implements OnInit, OnDestroy { 
    private sub: Subscription; 
    theUserProfile: FirebaseObjectObservable<UserProfile[]>; 

    constructor(private authService:AuthService, private af: AngularFire, private auth: FirebaseAuth) {} 

    ngOnInit() { 
    this.sub = this.authService.getUserProfile().subscribe(data => { 
     console.log('this is data:', data); 
     this.theUserProfile = data; 
    }) 
    } 

    ngOnDestroy() { 
    this.sub.unsubscribe(); 
    } 
} 

的問題是:我怎麼能assig n數據輸出到this.UserProfile? console.log('this is data:',data);給回this is data: FirebaseObjectObservable {_isScalar: false, _ref: U}

+0

您是否嘗試過鑄造?在調用你的autService之前添加'>',就像這樣:'this.sub = > this.authService.getUserProfile()'subscribe()' –

+0

Hi Fabio。感謝您的回答。我已經嘗試過你的示例代碼,沒有任何運氣。但你讓我走向正確的方向。我在下面添加了答案 –

回答

0

感謝法比奧的建議,我能夠回答我自己的問題。

需要在我的組件中更改以下內容。

export class HomeComponent implements OnInit, OnDestroy { 
    private sub: Subscription; 
    theUserProfile; 

    constructor(private authService:AuthService, private af: AngularFire, private auth: FirebaseAuth) {} 

    ngOnInit() { 
    this.sub = this.authService.getUserProfile().subscribe(data => { 
     console.log('this is data:', data); 
     this.theUserProfile = <FirebaseObjectObservable<UserProfile[]>>data; 
    }) 
相關問題