I have a method in my bug service that retrieves a single record from Firebase based on the bugId provided:
// This method is located in bugservice.ts
getBug(bugId: string): Promise<Bug> {
return this.bugsDbRef
.child(bugId)
.once('value')
.then(snapshot => {
const bugInfo = snapshot.val() as Bug;
bugInfo.id = snapshot.key;
}) as Promise<Bug>;
}
In my bugDetail component, I inject the bug service and call the above method as follows:
export class BugDetailComponent implements OnInit, OnDestroy {
private subscription: Subscription;
private bugDetail: Bug = new Bug(null,null,null,null,null,null,null,null,null); // To be populated from the service call to getBug
private bugId: string; // Obtained from the URL
constructor(private bugService: BugService, private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.configureForm();
this.subscription = this.activatedRoute.params.subscribe(
(param: any) => {
this.bugId = param['id'];
});
const t = this.bugService.getBug(this.bugId);
console.log(t);
}
}
However, when I log t to the console, the following output is displayed:
https://i.sstatic.net/iyPKW.png
Replacing t with
this.bugDetail = this.bugService.getBug(this.bugId);
Results in the error message:
Type 'Promise' is not assignable to type 'Bug'. Property 'id' is missing in type 'Promise'.
Could someone please guide me on how to access the bug details fetched from the service within the BugDetail Component?