{"record":{"id":"b8c42be44f3c9565","repo":"apify/crawlee","slug":"the-value-parameter-was-stringified-to-json-and","errorCode":null,"errorMessage":"The \"value\" parameter was stringified to JSON and returned undefined. Make sure you're not trying to stringify an undefined value.","messagePattern":"The \"value\" parameter was stringified to JSON and returned undefined\\. Make sure you're not trying to stringify an undefined value\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storages/key_value_store_codec.ts","lineNumber":63,"sourceCode":"    if (typeof value === 'string') {\n        return { value, contentType: 'text/plain; charset=utf-8' };\n    }\n\n    let serialized: string;\n    try {\n        // Format JSON to simplify debugging, the overheads with compression is negligible\n        serialized = jsonStringifyExtended(value as Dictionary, null, 2);\n    } catch (e) {\n        const error = e as Error;\n        // Give more meaningful error message\n        if (error.message?.includes('Invalid string length')) {\n            error.message = 'Object is too large';\n        }\n        throw new Error(`The \"value\" parameter cannot be stringified to JSON: ${error.message}`);\n    }\n\n    if (serialized === undefined) {\n        throw new Error(\n            'The \"value\" parameter was stringified to JSON and returned undefined. ' +\n                \"Make sure you're not trying to stringify an undefined value.\",\n        );\n    }\n\n    return { value: serialized, contentType: 'application/json; charset=utf-8' };\n}\n\n/**\n * Parses a Buffer or ArrayBuffer using the provided content type header.\n *\n * - application/json is returned as a parsed object.\n * - application/*xml and text/* are returned as strings.\n * - everything else is returned as original body.\n *\n * If the header includes a charset, the body will be stringified only\n * if the charset represents a known encoding to Node.js or Browser.\n *","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/storages/key_value_store_codec.ts#L45-L81","documentation":"If JSON.stringify returns undefined (which happens for top-level undefined, functions, or symbols), there is nothing storable, so the library throws this explicit error rather than writing an empty/meaningless record. It points out that the caller tried to stringify an undefined value.","triggerScenarios":"store.setValue('key', undefined); passing a function or symbol directly as value; calling a getter/helper that returns undefined (e.g. a missing variable or a misconfigured optional property like someObject.maybeField).","commonSituations":"Conditional state that was never assigned; destructuring typos producing undefined; refactors where a function reference was passed instead of its result (setValue('k', getData) instead of setValue('k', getData())).","solutions":["Check that the value is defined before calling setValue (log it or assert)","If the value can legitimately be absent, store null instead of undefined","If you passed a function by mistake, call it and store its result"],"exampleFix":"// before\nawait store.setValue('state', maybeState); // maybeState is undefined\n// after\nif (maybeState !== undefined) await store.setValue('state', maybeState);\nelse await store.setValue('state', null);","handlingStrategy":"validation","validationCode":"function assertDefined(value, key) {\n  if (value === undefined) throw new TypeError(`Refusing to store undefined value for key \"${key}\"`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await store.setValue(key, value);\n} catch (e) {\n  if (e.message.includes('returned undefined')) {\n    await store.setValue(key, null); // explicit fallback\n  } else throw e;\n}","preventionTips":["Log values before storing to catch undefined early","Use null (not undefined) for intentionally absent data","Dereference functions before storing: setValue(k, fn()) not setValue(k, fn)"],"tags":["serialization","json","undefined"],"backgroundTag":"json-serialization-failed","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}