withastro/astro · error · Error
Cannot convert undefined to an object.
Error message
Cannot convert undefined to an object.
What it means
`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.
Source
Thrown at packages/astro/src/core/cookies/cookies.ts:55
options?: AstroCookieSetOptions,
): void;
delete(key: string, options?: AstroCookieDeleteOptions): void;
}
const DELETED_EXPIRATION = new Date(0);
const DELETED_VALUE = 'deleted';
const responseSentSymbol = Symbol.for('astro.responseSent');
const identity = (value: string) => value;
class AstroCookie implements AstroCookieInterface {
public value: string;
constructor(value: string) {
this.value = value;
}
json() {
if (this.value === undefined) {
throw new Error(`Cannot convert undefined to an object.`);
}
return JSON.parse(this.value);
}
number() {
return Number(this.value);
}
boolean() {
if (this.value === 'false') return false;
if (this.value === '0') return false;
return Boolean(this.value);
}
}
class AstroCookies implements AstroCookiesInterface {
#request: Request;
#requestValues: Record<string, string | undefined> | null;
#outgoing: Map<string, [string, string, boolean]> | null;
#consumed: boolean;View on GitHub (pinned to d081033d5f)
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.
Example fix
// before
const data = Astro.cookies.get('cart').json();
// after
const cart = Astro.cookies.get('cart');
const data = cart ? cart.json() : {}; Defensive patterns
Strategy: type-guard
Validate before calling
const c = Astro.cookies.get('key'); if (!c) return; const v = c.json(); Type guard
function hasCookieValue(c) { return c != null && c.value !== undefined && c.value !== ''; } Try / catch
try { data = cookie.json(); } catch (e) { if (/undefined/.test(e.message)) data = {}; else throw e; } Prevention
- Check cookie presence before deserializing.
- Default to empty objects when a cookie is absent.
- Avoid constructing AstroCookie with undefined values.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- ResponseSentError
- AstroResponseHeadersReassigned
- UnknownContentCollectionError
- RenderUndefinedEntryError
- CacheNotEnabled
AI-assisted analysis of withastro/astro@d081033d5f (2026-08-12).
Data as JSON: /api/errors/6da1e17dff301aee.
Report an issue: GitHub.