Skip to content

extract crashes on singleton collections (getCollectionItems spreads a non-array response) #144

@zerothirteenstudio

Description

@zerothirteenstudio

Summary

extract (with content) crashes when the source instance has singleton collections. The content extractor calls readItems() for every collection and spreads the response into an array, but a singleton returns a single object (not an array), so the spread throws:

Error: Spread syntax requires ...iterable[Symbol.iterator] to be a function
Context: {"collection":"globals","function":"getDataFromCollection"}

The crash is fatal and stops content extraction — no content is written for the singleton or for any collection alphabetically after it.

Steps to reproduce

  1. A Directus instance with at least one singleton collection (e.g. the Agency OS template's globals, os_settings, pages_blog, pages_projects).
  2. directus-template-cli extract -p --templateName="x" --templateLocation=./t (content enabled, the default).
  3. Crashes at the first singleton (globals).

Cause

dist/lib/extract/extract-content.jsgetCollectionItems:

const response = await api.client.request(readItems(collection, { limit: PAGE_SIZE, page }));
items.push(...response);              // singleton response is an object, not iterable
if (response.length < PAGE_SIZE) break;

For a singleton, readItems(collection) resolves to the item object, so ...response (spread) and response.length are both invalid.

Suggested fix

Guard for non-array responses:

const response = await api.client.request(readItems(collection, { limit: PAGE_SIZE, page }));
const rows = Array.isArray(response) ? response : (response ? [response] : []);
items.push(...rows);
if (rows.length < PAGE_SIZE) break;

(Alternatively, detect singletons from the collection meta and use readSingleton.) Verified locally — patching this lets a full extract-with-content complete cleanly on an instance with the four singletons above.

Environment

  • directus-template-cli 0.8.0
  • Node 24, darwin-arm64
  • Directus 11.17.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions