Zie619/n8n-workflows · error · Error

No category in Code2

Error message

No category in Code2

What it means

Thrown by 'Picks Less Used' (workflow 0263) when $items('Category Filter') returns an empty list. The upstream Category Filter is itself a Code node that filters Postgres category rows by an excludeIds list; if it emits zero items (all rows excluded, or its own Postgres input empty), this node aborts. The sibling 'Selecting recent' Postgres node has alwaysOutputData: true, so an empty used set would not throw here — only an empty category set does.

Source

Thrown at workflows/Postgres/0263_Postgres_Code_Automation_Webhook.json:574

          "id": "JKCOXnEh1Bqg4Gad",
          "name": "YOUR_POSTGRES_CREDENTIAL"
        }
      },
      "executeOnce": true,
      "typeVersion": 2.6,
      "alwaysOutputData": true,
      "notes": "This postgres node performs automated tasks as part of the workflow."
    },
    {
      "id": "98f3ec81-f1f9-425b-83b0-2d5732acb19e",
      "name": "Picks Less Used",
      "type": "n8n-nodes-base.code",
      "position": [
        80,
        -560
      ],
      "parameters": {
        "jsCode": "const categories = $items(\"Category Filter\");\nconst usedRows = $items(\"Selecting recent\");\n\nif (!categories || categories.length === 0) {\n  throw new Error(\"No category in Code2\");\n}\n\nif (!usedRows || usedRows.length === 0) {\n  return [categories[0]];\n}\n\nconst usedMap = new Map(\n  usedRows.map(row => {\n    const id = row.json.category_id;\n    const time = new Date(row.json.last_used_at || row.json.used_at).getTime();\n    return [id, time];\n  })\n);\n\nlet selected = null;\nlet minTime = Infinity;\n\nfor (const cat of categories) {\n  const id = cat.json.id;\n  const lastUsed = usedMap.get(id) ?? 0;\n\n  if (lastUsed < minTime) {\n    minTime = lastUsed;\n    selected = cat;\n  }\n}\n\nreturn [selected || categories[0]];"
      },
      "typeVersion": 2,
      "notes": "This code node performs automated tasks as part of the workflow."
    },
    {
      "id": "8f94b488-d3fb-4016-a8ac-ed0e13f78190",
      "name": "10 latest headlines",
      "type": "n8n-nodes-base.postgres",
      "position": [
        400,
        -200
      ],
      "parameters": {
        "query": "SELECT name, description \nFROM used_categories \nWHERE category_id = {{ $json.id }}\nORDER BY used_at DESC \nLIMIT 10;",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {

View on GitHub (pinned to 94007c1445)

Solutions

  1. Run the Postgres node feeding Category Filter and confirm it returns rows; then run the filter node and check how many survive excludeIds.
  2. Shrink/adjust excludeIds or add categories so at least one remains.
  3. Replace the hard throw with a no-op or default category when empty is legitimate (see exampleFix).
  4. Verify the Postgres credential/database — the template ships with 'YOUR_POSTGRES_CREDENTIAL'.

Example fix

// before
if (!categories || categories.length === 0) {
  throw new Error("No category in Code2");
}

// after
const usable = (categories || []).filter(c => c && c.json && c.json.id != null);
if (usable.length === 0) {
  throw new Error(`No usable category after excludeIds filter; input items: ${categories.length}`);
}
Defensive patterns

Strategy: validation

Validate before calling

const categories = ($items('Category Filter') || []).filter(c => c?.json?.id != null);
if (categories.length === 0) {
  throw new Error(`No usable category; raw items: ${$items('Category Filter').length}, excludeIds covered all`);
}

Type guard

function isCategoryItem(item) {
  return item?.json && item.json.id != null;
}

Prevention

When it happens

Trigger: The categories Postgres query returns nothing, or every returned category id falls inside excludeIds [1,11,12,13,15,17,18,36,37,38,39] so the filter node outputs []. Renaming the 'Category Filter' node would make $items throw a different error, so this message specifically means: filter ran, produced zero items.

Common situations: Fresh database where used_categories/categories tables are empty; excludeIds covering the entire current category set after new categories were removed; Postgres credential pointing at the wrong database; timezone/maintenance job truncating the category table.

Related errors


AI-assisted analysis of Zie619/n8n-workflows@94007c1445 (2026-08-15). Data as JSON: /api/errors/8c70ccf929c5e52f. Report an issue: GitHub.