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}
您是否嘗試過鑄造?在調用你的autService之前添加'>',就像這樣:'this.sub = > this.authService.getUserProfile()'subscribe()' –
Hi Fabio。感謝您的回答。我已經嘗試過你的示例代碼,沒有任何運氣。但你讓我走向正確的方向。我在下面添加了答案 –