The most common cause of the «Cannot read properties of undefined (reading „forEach“)» error is trying to iterate over an undefined or null array or object. 1
To avoid this error, make sure that the array or object you are trying to iterate over is defined and initialized properly. 1
Other common causes:
- Async operations. 1 If you are working with asynchronous code, make sure to handle promises or callbacks appropriately to avoid accessing undefined values. 1
- Accessing an array index that doesn't exist. 25 The error often occurs when trying to access an empty array at an index that doesn't exist, which returns undefined. 2
- Assigning the output of a function that doesn't return anything to a variable. 2 Many built-in methods that mutate an object in place return undefined. 2