Skip to content

JavaScript errorJavaScript: “TypeError: x is not iterable”

Code throws TypeError: data is not iterable, object is not iterable (cannot read property Symbol(Symbol.iterator)), or undefined is not iterable, or, when a function call is the source, TypeError: useThing is not a function or its return value is not iterable. Something tried to loop over, spread or array-destructure a value that is not a list: a plain object, a promise that was never awaited, or undefined.

Short answer

Log the value just before the line that throws. If it is a plain object, loop over Object.entries(obj) or Object.values(obj) instead. If it is a Promise, add await. If it is undefined, the data has not loaded or the property name is wrong, so default it (list ?? []) and fix where it comes from. For const [a, b] = something, check that something really returns an array, not an object.

What the value really is, and the fix

  1. 01

    A plain object in for...of or spread

    “In JavaScript, Objects are not iterable unless they implement the iterable protocol.” MDN, TypeError: 'x' is not iterable

    Objects are not lists. API responses often wrap the list ({ items: [...] }), and maps of id to record are objects too. Loop over the property that holds the array, or over Object.entries.

    for (const item of response.items) { /* ... */ }
    
    for (const [id, user] of Object.entries(usersById)) { /* ... */ }
  2. 02

    undefined: data that has not arrived

    On the first render, before a fetch finishes, or when a property name is wrong, the value is undefined. Give it a default where it enters your code, and fix the source if it should always be there.

    const items = data?.items ?? [];
    return items.map((item) => <Row key={item.id} item={item} />);
  3. 03

    A Promise that was never awaited

    “In order to be iterable, an object must implement the [Symbol.iterator]() method” MDN, Iteration protocols

    An async function returns a Promise, not the array inside it. A Promise is an object without that method, so looping over it throws. Await it first.

    // wrong
    const rows = getRows();
    for (const row of rows) {}
    
    // right
    const rows = await getRows();
  4. 04

    Array destructuring from something that is not an array

    const [value, setValue] = useMyHook() throws when the hook returns an object or nothing. React context is a common case: a provider that passes an object, or a component outside the provider that gets the default value. Destructure with braces for objects, and check the provider.

    const { user, setUser } = useContext(UserContext);

Checked against the vendor’s docs on : MDN: TypeError: 'x' is not iterable; MDN: Iteration protocols.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: JavaScript “x is not a function” · “Cannot read properties of undefined” · “Cannot destructure property of undefined”

Questions

More on this error

Why does Promise.all say it is not iterable?

Promise.all takes one array of promises. Passing promises as separate arguments, or passing an object, makes it try to iterate something that is not a list. Wrap them in an array.

Is a string iterable?

Yes. for...of over a string gives one character at a time, which is often a sign the data was JSON text that still needs JSON.parse.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.