TypeError: data.forEach is not a function
Youβre calling .forEach() on something that isnβt an array.
Fix 1: Itβs an object, not an array
const data = { a: 1, b: 2 };
// β Objects don't have forEach
data.forEach(...) // TypeError!
// β
Use Object.entries, Object.keys, or Object.values
Object.entries(data).forEach(([key, value]) => {
console.log(key, value);
});
Fix 2: Itβs a NodeList, not an array
const items = document.querySelectorAll('.item');
// β
NodeList has forEach in modern browsers
items.forEach(item => ...);
// β
Convert to array for older browsers
Array.from(items).forEach(item => ...);
[...items].forEach(item => ...);
Fix 3: The data is null or undefined
// β API returned null
const data = null;
data.forEach(...) // TypeError!
// β
Default to empty array
(data || []).forEach(item => ...);
Fix 4: Itβs a string
const text = 'hello';
// β Strings don't have forEach
text.forEach(...) // TypeError!
// β
Use split or spread
[...text].forEach(char => console.log(char));