I am struggling to retrieve a single record by uid from my Firebase database in an Angular 2 app. The issue I am facing is that the profile variable always returns as undefined
. Can someone guide me on the correct approach to solving this? Thank you!
My code:
Profile class
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 service
@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]));
}
}
Profile component
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;
}
);
}
}