I've been working with Angular CLI 6 and Angularfire2, and my code is functioning well to display data in the template using a typical ngFor loop and an asynchronous pipe. However, I also need to utilize the data for a Google graph, which requires data from the TypeScript side. How can I convert my observable into an array in the TypeScript as outlined below?
If necessary, I can provide a Plunker for demonstration.
Within my FireDatabase:
--ppss
--pps1key
--treatment: value1
--DateA: DateA1
--Date B: DateB1
--pps2key
--treatment: value2
--DateA: DateA2
--Date B: DateB2
I aim to display data in my //component.ts in the following format:
this.data1 = [
['treatment', 'dateA', 'dateB'],
[ 'Treatment.value1', new DateA1(), new DateB1()],
[ 'Treatment.value2', new DateA2(), new DateB2()]
];
From my service, I send an observable like this:
getPPSByPatientid(Patientid: string)
{
return this.database.list('/ppss', ref =>
ref.orderByChild("Patientid").equalTo(Patientid)).valueChanges();
}
Despite my efforts, I am encountering multiple errors with the following implementation:
patientid: string;
ppssToDisplay;
data1: any[];
ngOnInit() {
this.route.params.forEach((urlParameters) => {
this.patientid = urlParameters['id'];});
this.ppssToDisplay = this.ppssService.getPPSByPatientid(this.patientid);
let interestingFields = [ 'treatment', 'dateA', 'dateB'];
this.ppssToDisplay.subscribe(obj => {
this.data1 = [
interestingFields,
interestingFields.map(field => obj[field]),
];
console.log(this.data1);
});
Error Encountered:
core.js:1598 ERROR Error: Uncaught (in promise): Error: Not an array Error: Not an array