1
我想通過Angular 2應用程序中的我的Firebase數據庫中的uid獲取一條記錄。問題是我總是在配置文件變量中獲得undefinied
。你能告訴我正確的方法嗎?謝謝! 我的代碼:如何從Angular 2中的Firebase數據庫中獲取一條記錄
Profile類
export class Profile{
constructor(public $key:string, public userId:string, public description:string, public avatarUrl: string){
}
static parseFromJson({$key, userId, description, avatarUrl}):Profile{
return new Profile($key, userId, description, avatarUrl);
}
}
Profiles服務
@Injectable()
export class ProfilesService {
constructor(private db: AngularFireDatabase) {
}
getUserByUserId(userId:string): Observable<Profile>{
return this.db.list('profiles',{
query: {
orderByChild: 'userId',
equalTo: userId
}
}).map(result => Profile.parseFromJson(result[0]));
}
}
輪廓組件
export class ProfileComponent {
profile: Profile;
uid: string;
constructor(private profileService: ProfilesService, private af: AngularFire) {
this.af.auth.subscribe(auth => this.uid = auth.uid);
this.profileService.getUserByUserId(this.uid).subscribe(
result => {
this.profile = result;
}
);
}
}