How to fix Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'push') in TypeScript

I had the following mysterious error when looping over an API call and populating an array:

Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'push')

Declaration was at first:

bookIds!: any[];

This error occurs in angular when you didn't initialise the array blank.

Solution

Initialize an empty one:

  bookIds: any = [ ];

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!