The issue at hand is with the Typescript part of an Angular app.
I have a data array I've received from a subscription; it contains several (but not a consistent or predictable number of) objects with members "titleName" and "ID". I need to add the member "toggle", which will be interacted with within the component. Due to the database call in question and its other uses, I can't realistically add "toggle" on the services side. I've tried the following:
titlesraw;
titles: any;
...
getTitles() {
this.getTitles.subscribe(
data => {
this.titlesraw = data;
console.log(this.titlesraw); //console reads what I expect
for (var i = 0; i < this.titlesraw.length; i++) {
let title = {
id: this.titlesraw[i]["ID"];
titleName: this.titlesraw[i]["titleName"];
toggle: true;
}
console.log(title); //console reads what I expect
this.titles.push(title);
}
}
)
}
When I run the code, I get the following error:
ERROR TypeError: Cannot read properties of undefined (reading 'push') at <line with this.titles.push(title)>
I think this is because I'm not declaring titles correctly. What do I need to do differently?
EDIT: I've changed titles: any; to titles: any[] = [];, and I get the following error instead:
Cannot read properties of undefined (reading 'ID') at <line with this.titles.push(title)>.
Again, when I do the printout of the temporary variable title, it shows me exactly what I expect.
titlesbut didn't assign value to it. It should betitles: any[] = [];