{"record":{"id":"8c70ccf929c5e52f","repo":"Zie619/n8n-workflows","slug":"no-category-in-code2","errorCode":null,"errorMessage":"No category in Code2","messagePattern":"No category in Code2","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"workflows/Postgres/0263_Postgres_Code_Automation_Webhook.json","lineNumber":574,"sourceCode":"          \"id\": \"JKCOXnEh1Bqg4Gad\",\n          \"name\": \"YOUR_POSTGRES_CREDENTIAL\"\n        }\n      },\n      \"executeOnce\": true,\n      \"typeVersion\": 2.6,\n      \"alwaysOutputData\": true,\n      \"notes\": \"This postgres node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"98f3ec81-f1f9-425b-83b0-2d5732acb19e\",\n      \"name\": \"Picks Less Used\",\n      \"type\": \"n8n-nodes-base.code\",\n      \"position\": [\n        80,\n        -560\n      ],\n      \"parameters\": {\n        \"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]];\"\n      },\n      \"typeVersion\": 2,\n      \"notes\": \"This code node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"8f94b488-d3fb-4016-a8ac-ed0e13f78190\",\n      \"name\": \"10 latest headlines\",\n      \"type\": \"n8n-nodes-base.postgres\",\n      \"position\": [\n        400,\n        -200\n      ],\n      \"parameters\": {\n        \"query\": \"SELECT name, description \\nFROM used_categories \\nWHERE category_id = {{ $json.id }}\\nORDER BY used_at DESC \\nLIMIT 10;\",\n        \"options\": {},\n        \"operation\": \"executeQuery\"\n      },\n      \"credentials\": {","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/workflows/Postgres/0263_Postgres_Code_Automation_Webhook.json#L556-L592","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the Postgres node feeding Category Filter and confirm it returns rows; then run the filter node and check how many survive excludeIds.","Shrink/adjust excludeIds or add categories so at least one remains.","Replace the hard throw with a no-op or default category when empty is legitimate (see exampleFix).","Verify the Postgres credential/database — the template ships with 'YOUR_POSTGRES_CREDENTIAL'."],"exampleFix":"// before\nif (!categories || categories.length === 0) {\n  throw new Error(\"No category in Code2\");\n}\n\n// after\nconst usable = (categories || []).filter(c => c && c.json && c.json.id != null);\nif (usable.length === 0) {\n  throw new Error(`No usable category after excludeIds filter; input items: ${categories.length}`);\n}","handlingStrategy":"validation","validationCode":"const categories = ($items('Category Filter') || []).filter(c => c?.json?.id != null);\nif (categories.length === 0) {\n  throw new Error(`No usable category; raw items: ${$items('Category Filter').length}, excludeIds covered all`);\n}","typeGuard":"function isCategoryItem(item) {\n  return item?.json && item.json.id != null;\n}","tryCatchPattern":null,"preventionTips":["Keep excludeIds smaller than the live category set, or store exclusions in the DB.","Distinguish 'query returned 0 rows' from 'filter removed everything' in the error message.","Seed the categories table before enabling scheduled runs on a fresh database."],"tags":["n8n","postgres","empty-results","filter","data-validation"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}