{"record":{"id":"32fc3f733d3d41bb","repo":"actualbudget/actual","slug":"order-by-contains-an-empty-field","errorCode":null,"errorMessage":"--order-by contains an empty field","messagePattern":"--order-by contains an empty field","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/query.ts","lineNumber":19,"sourceCode":"import * as api from '@actual-app/api';\nimport type { Command } from 'commander';\n\nimport { withConnection } from '#connection';\nimport { readJsonInput } from '#input';\nimport { printOutput } from '#output';\nimport { isRecord, parseIntFlag } from '#utils';\n\n/**\n * Parse order-by strings like \"date:desc,amount:asc,id\" into\n * AQL orderBy format: [{ date: 'desc' }, { amount: 'asc' }, 'id']\n */\nexport function parseOrderBy(\n  input: string,\n): Array<string | Record<string, string>> {\n  return input.split(',').map(part => {\n    const trimmed = part.trim();\n    if (!trimmed) {\n      throw new Error('--order-by contains an empty field');\n    }\n    const colonIndex = trimmed.indexOf(':');\n    if (colonIndex === -1) {\n      return trimmed;\n    }\n    const field = trimmed.slice(0, colonIndex).trim();\n    if (!field) {\n      throw new Error(\n        `Invalid order field in \"${trimmed}\". Field name cannot be empty.`,\n      );\n    }\n    const direction = trimmed.slice(colonIndex + 1);\n    if (direction !== 'asc' && direction !== 'desc') {\n      throw new Error(\n        `Invalid order direction \"${direction}\" for field \"${field}\". Expected \"asc\" or \"desc\".`,\n      );\n    }\n    return { [field]: direction };","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/actualbudget/actual/blob/d4334cb6e6123f4d3bcea1ad6166608884c7e658/packages/cli/src/commands/query.ts#L1-L37","documentation":"`parseOrderBy` splits the `--order-by` value on commas and each comma-separated part must name a field. An empty segment (from a leading/trailing/double comma) throws this error, because an empty field name cannot be translated into an `order_by` query expression.","triggerScenarios":"Passing `--order-by \"\"`, `--order-by \",date\"`, `--order-by \"date,\"` or `--order-by \"date,,amount\"` — any input that yields an empty segment after splitting on commas and trimming.","commonSituations":"Building the flag from a joined list in a script where the last element is empty (`\"date,\"` from `join(',', [...ids, ''])`); accidental trailing commas when hand-editing commands.","solutions":["Remove empty segments and stray commas: `--order-by date` or `--order-by date:desc,amount`.","If assembling the list programmatically, filter out empty strings before joining with commas."],"exampleFix":"// before\nactual pay --order-by \"date,\"\n// after\nactual pay --order-by \"date\"","handlingStrategy":"validation","validationCode":"const parts = orderByStr.split(',').map(s => s.trim()).filter(Boolean);\nif (parts.length === 0) throw new Error('--order-by must name at least one field');","typeGuard":null,"tryCatchPattern":"try {\n  await cli(['query', '--table', t, '--order-by', orderByStr]);\n} catch (e) {\n  if (e.message.includes('--order-by contains an empty field')) {\n    console.error('Remove empty segments/stray commas from --order-by');\n  }\n}","preventionTips":["Never leave a leading/trailing/double comma in --order-by.","Filter empty strings when joining field lists in scripts.","Keep sort specs short and hand-checked: date, date:desc,amount."],"tags":["cli","validation","query"],"backgroundTag":"invalid-flag-value","analyzedSha":"d4334cb6e6123f4d3bcea1ad6166608884c7e658","analyzedAt":"2026-08-29T01:02:11.213Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}