{"record":{"id":"9efb321d770fba97","repo":"withastro/astro","slug":"contentloaderinvaliddataerror","errorCode":"ContentLoaderInvalidDataError","errorMessage":"**${collection}** entry is missing an ID.\nEntry missing ID:\n${raw}","messagePattern":"\\*\\*(.+?)\\*\\* entry is missing an ID\\.\nEntry missing ID:\n(.+?)","errorType":"error_code","errorClass":"AstroError","httpStatus":null,"severity":"error","filePath":"packages/astro/src/content/content-layer.ts","lineNumber":505,"sourceCode":"\n\t\tconst entry = Array.isArray(unsafeData)\n\t\t\t? unsafeData[firstPathItem as number]\n\t\t\t: unsafeData[firstPathItem as string];\n\n\t\tthrow new AstroError({\n\t\t\t...AstroErrorData.ContentLoaderReturnsInvalidId,\n\t\t\tmessage: AstroErrorData.ContentLoaderReturnsInvalidId.message(context.collection, entry),\n\t\t});\n\t}\n\n\tconst data = parsedData.data;\n\n\tcontext.store.clear();\n\n\tif (Array.isArray(data)) {\n\t\tfor (const raw of data) {\n\t\t\tif (!raw.id) {\n\t\t\t\tthrow new AstroError({\n\t\t\t\t\t...AstroErrorData.ContentLoaderInvalidDataError,\n\t\t\t\t\tmessage: AstroErrorData.ContentLoaderInvalidDataError.message(\n\t\t\t\t\t\tcontext.collection,\n\t\t\t\t\t\t`Entry missing ID:\\n${JSON.stringify({ ...raw, id: undefined }, null, 2)}`,\n\t\t\t\t\t),\n\t\t\t\t});\n\t\t\t}\n\t\t\tconst item = await context.parseData({ id: raw.id, data: raw });\n\t\t\tcontext.store.set({ id: raw.id, data: item });\n\t\t}\n\t\treturn;\n\t}\n\tif (typeof data === 'object') {\n\t\tfor (const [id, raw] of Object.entries(data)) {\n\t\t\tif (raw.id && raw.id !== id) {\n\t\t\t\tthrow new AstroError({\n\t\t\t\t\t...AstroErrorData.ContentLoaderInvalidDataError,\n\t\t\t\t\tmessage: AstroErrorData.ContentLoaderInvalidDataError.message(","sourceCodeStart":487,"sourceCodeEnd":523,"githubUrl":"https://github.com/withastro/astro/blob/d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca/packages/astro/src/content/content-layer.ts#L487-L523","documentation":"After `simpleLoader` validates the schema, it iterates the returned array and explicitly checks each `raw.id`. If an entry has a falsy `id`, it throws `ContentLoaderInvalidDataError` with the offending entry serialized. This is a defensive check beyond schema validation to produce a clear, actionable message.","triggerScenarios":"A function loader returns an array where one or more entries have `id: undefined`, `id: null`, `id: ''`, or no `id` at all.","commonSituations":"Some CMS items lack a slug; partial DB rows where the id column is null; conditional `id` assignment that drops the field for certain records.","solutions":["Filter or repair entries before returning them so every entry has a non-empty string id.","Generate a stable id (e.g. from a slug or hash) when the source lacks one.","Skip entries without an id explicitly if they should not be indexed."],"exampleFix":"// before\nconst loader = () => fetch('/api/posts').then(r => r.json());\n// after\nconst loader = () => fetch('/api/posts').then(r => r.json()).then(arr => arr.filter(p => p.id).map(p => ({ id: p.id, ...p })));","handlingStrategy":"validation","validationCode":"const out = await loader();\nif (Array.isArray(out)) {\n  for (const raw of out) if (!raw.id) throw new Error('Entry missing id');\n}","typeGuard":"function everyEntryHasId(arr) {\n  return arr.every(e => !!e?.id);\n}","tryCatchPattern":null,"preventionTips":["Filter out entries without ids before returning from the loader.","Generate stable ids when the source lacks them.","Test loaders against edge-case inputs (null id, missing field)."],"tags":["content-collections","loader","validation","data-integrity"],"backgroundTag":null,"analyzedSha":"d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca","analyzedAt":"2026-08-12T13:37:29.035Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}