I have a Angular service that provides access to Firebase. I have a component in which I want to get the data from Firebase, put that information in an array, and then call some functions on that now initialized array. I'm new to Angular, so I'm not exactly sure how I should implement this behavior. I know subscribe() is asynchronous, and I need to implement my functions such that they are sensitive to this.
graph.component.ts
import { Component, OnInit, OnChanges, ViewChild, ElementRef, Input, ViewEncapsulation } from '@angular/core';
import { EscortService } from '../services/escort/escort.service';
import { Escort } from '../data/escort.data';
import * as d3 from 'd3';
@Component({
selector: 'app-graph',
templateUrl: './graph.component.html',
styleUrls: ['./graph.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class GraphComponent implements OnInit {
escortList : Escort[] = [];
constructor(private escortService : EscortService, private element: ElementRef){
}
ngOnInit(){
this.getData();
this.generateBarChart();
}
getData(){
var esc = this.escortService.getData();
esc.snapshotChanges().subscribe(item => {
this.escortList = [];
item.forEach(element => {
var y = element.payload.toJSON();
y["$key"] = element.key;
var currentEscort = (y as Escort);
if(currentEscort.status == 'Completed'){
console.log("escort-list -> ngOnInit() : currentEscort.status = " + currentEscort.status);
this.escortList.push(currentEscort);
}
});
});
}