{"record":{"id":"6da1e17dff301aee","repo":"withastro/astro","slug":"cannot-convert-undefined-to-an-object","errorCode":null,"errorMessage":"Cannot convert undefined to an object.","messagePattern":"Cannot convert undefined to an object\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/astro/src/core/cookies/cookies.ts","lineNumber":55,"sourceCode":"\t\toptions?: AstroCookieSetOptions,\n\t): void;\n\tdelete(key: string, options?: AstroCookieDeleteOptions): void;\n}\n\nconst DELETED_EXPIRATION = new Date(0);\nconst DELETED_VALUE = 'deleted';\nconst responseSentSymbol = Symbol.for('astro.responseSent');\n\nconst identity = (value: string) => value;\n\nclass AstroCookie implements AstroCookieInterface {\n\tpublic value: string;\n\tconstructor(value: string) {\n\t\tthis.value = value;\n\t}\n\tjson() {\n\t\tif (this.value === undefined) {\n\t\t\tthrow new Error(`Cannot convert undefined to an object.`);\n\t\t}\n\t\treturn JSON.parse(this.value);\n\t}\n\tnumber() {\n\t\treturn Number(this.value);\n\t}\n\tboolean() {\n\t\tif (this.value === 'false') return false;\n\t\tif (this.value === '0') return false;\n\t\treturn Boolean(this.value);\n\t}\n}\n\nclass AstroCookies implements AstroCookiesInterface {\n\t#request: Request;\n\t#requestValues: Record<string, string | undefined> | null;\n\t#outgoing: Map<string, [string, string, boolean]> | null;\n\t#consumed: boolean;","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/withastro/astro/blob/d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca/packages/astro/src/core/cookies/cookies.ts#L37-L73","documentation":"`AstroCookie.json()` parses the cookie's string value with `JSON.parse`, but first guards against an `undefined` value because `JSON.parse(undefined)` yields `Unexpected token u` — a confusing message. By throwing the explicit 'Cannot convert undefined to an object.' the API communicates that no cookie value exists to deserialize. Note the guard compares against `undefined` strictly, which is only possible when the cookie was constructed without a value.","triggerScenarios":"Calling `.json()` on an `AstroCookie` whose `value` is `undefined` — typically when a cookie accessor returned a placeholder/empty cookie. In practice the `AstroCookie` wrapper is constructed with a string, so this fires when a code path constructs `new AstroCookie(undefined as any)` or a similar coercion.","commonSituations":"A library/integration building an `AstroCookie` from an unguarded lookup; deserializing a cookie whose value was deleted/emptied; TypeScript bypass (`as any`) hiding an undefined.","solutions":["Check the cookie exists and has a value before calling `.json()`: `const c = Astro.cookies.get('data'); if (c) { const v = c.json(); }`.","Ensure the code constructing `AstroCookie` passes a real string (default to `''`).","If using `.get()` returns undefined, treat absence explicitly rather than coercing."],"exampleFix":"// before\nconst data = Astro.cookies.get('cart').json();\n\n// after\nconst cart = Astro.cookies.get('cart');\nconst data = cart ? cart.json() : {};","handlingStrategy":"type-guard","validationCode":"const c = Astro.cookies.get('key'); if (!c) return; const v = c.json();","typeGuard":"function hasCookieValue(c) { return c != null && c.value !== undefined && c.value !== ''; }","tryCatchPattern":"try { data = cookie.json(); } catch (e) { if (/undefined/.test(e.message)) data = {}; else throw e; }","preventionTips":["Check cookie presence before deserializing.","Default to empty objects when a cookie is absent.","Avoid constructing AstroCookie with undefined values."],"tags":["cookies","runtime","api"],"backgroundTag":null,"analyzedSha":"d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca","analyzedAt":"2026-08-12T13:37:29.035Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}