After retrieving a collection of data documents, I am iterating through them to form an object named 'Item'; each Item comprises keys for 'amount' and 'id'.
My goal is to add each created Item object to an array called 'Items'. However, when I initialize this array at the beginning (currently declared as var itemObjects: [Item]) and proceed to push each item like so:
snapshot.forEach((doc: any) => {
let docData = doc.data()
let items = docData.items
items.forEach((item: any) => {
let itemObject = new Item(item.amount, item.transactionType)
console.log("converted item to Item Object:", itemObject)
itemObjects.push(itemObject)
})
An error occurs: Unhandled error TypeError: Cannot read properties of undefined (reading 'push')\n
I suspect that my initialization of the variable array is incorrect. Any assistance would be greatly appreciated. Thank you.
EDIT- Here's the additional code snippet (pertaining to the Item class):
interface IItem {
amount: number
id: string
}
export class Item implements IItem {
amount: number
id: string
constructor(amount: number, id: string) {
this.amount = amount
this.id = id
}
}