Error Encyclopedia

Collection used in @for is not iterable

A @for loop repeats over an array, or over a value that implements the iterable protocol, such as a Map, a Set or a generator. This error means the collection was neither, most often a plain object:

@Component({
  template: `@for (item of items; track item) {
    {{ item }}
  }`,
})
class Test {
  items = {first: 'a', second: 'b'};
}

null and undefined render nothing instead of throwing, so this error only reports a value that is present but cannot be iterated.

Fixing the error

To repeat over the entries of a plain object, use the KeyValue pipe:

@Component({
  imports: [KeyValuePipe],
  template: `@for (item of items | keyvalue; track item.key) {
    {{ item.key }}: {{ item.value }}
  }`,
})
class Test {
  items = {first: 'a', second: 'b'};
}

If the collection comes from an API call or another dynamic source, check that the value is the list itself. A response that wraps the list in another object, such as {data: [...]}, is a common cause.