2016-10-28 35 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; 
    })