I am currently working on creating a sample model in Angular to fetch data from the backend. However, I am encountering an error related to arrays.
abc.ts
export class abc {
date: Date;
time: string;
ABC_Info: [{
item: string,
quantity: number,
a: number
},
{
item: string,
quantity: number,
a: number
}
]
constructor(date: Date, time: string, item: string, quantity: number, a: number) {
this.meal_time = meal_time;
this.date = date;
this.time = time;
this.ABC_Info[0].item = item;
this.ABC_Info[0].quantity = quantity;
this.ABC_Info[0].carbs = a;
this.ABC_Info[1].item = item;
this.ABC_Info[1].quantity = quantity;
this.ABC_Info[1].carbs = a;
}
}
abc.service.ts
import { abc} from './models/abc';
getABCs(start ? : moment.Moment, end ? : moment.Moment): Observable < abc[] > {
let params = new HttpParams();
if (start) {
params = params.append('start', start.toISOString());
}
if (end) {
params = params.append('end', end.toISOString());
}
return this.baseService.getParams('/api/abc/getAll', params)
.pipe(
map(res => res as Object[] || []),
map(bolus => bolus.map(k => new abc(
new Date(k['date']),
k['time'],
k['item'],
k['quantity'],
k['a']))
)
)
}
Error in browser console
ERROR TypeError: Cannot read property '0' of undefined at new Bolus(abc.ts: 39) at abc.service.ts: 33 at Array.map( < anonymous > ) at MapSubscriber.project(carb.service.ts: 33) at MapSubscriber.push.. / node_modules / rxjs / _esm5 / internal / operators / map.js.MapSubscriber._next(map.js: 35) at MapSubscriber.push.. / node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next(Subscriber.js: 54) at MapSubscriber.push.. / node_modules / rxjs / _esm5 / internal / operators / map.js.MapSubscriber._next(map.js: 41) at MapSubscriber.push.. / node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next(Subscriber.js: 54) at MergeMapSubscriber.push.. / node_modules / rxjs / _esm5 / internal / operators / mergeMap.js.MergeMapSubscriber.notifyNext(mergeMap.js: 84) at InnerSubscriber.push.. / node_modules / rxjs / _esm5 / internal / InnerSubscriber.js.InnerSubscriber._next(InnerSubscriber.js: 15)