{"record":{"id":"dc3e949a58aa34b1","repo":"apify/crawlee","slug":"ownedorinjected-value-is-not-initialized-yet","errorCode":null,"errorMessage":"OwnedOrInjected value is not initialized yet","messagePattern":"OwnedOrInjected value is not initialized yet","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/owned_or_injected.ts","lineNumber":70,"sourceCode":"    get isOwned(): boolean {\n        return this.#owned;\n    }\n\n    /**\n     * Whether a value is currently available. `false` for an owned slot whose default hasn't been built yet\n     * (e.g. a lazily-opened request queue before its first use).\n     */\n    get isPresent(): boolean {\n        return this.#present;\n    }\n\n    /**\n     * The resolved instance, typed as the public `Injected` type. Throws if the value is not present yet — callers that\n     * expect a lazily-filled owned slot should read {@apilink OwnedOrInjected.maybeValue|`maybeValue`} instead.\n     */\n    get value(): Injected {\n        if (!this.#present) {\n            throw new Error('OwnedOrInjected value is not initialized yet');\n        }\n\n        return this.#value as Injected;\n    }\n\n    /**\n     * The resolved instance, or `undefined` when a lazily-filled owned slot hasn't been built yet. The non-throwing\n     * counterpart to {@apilink OwnedOrInjected.value|`value`} — pairs naturally with `?? fallback` so callers can read\n     * a possibly-empty slot without the `isPresent ? value : …` dance.\n     */\n    get maybeValue(): Injected | undefined {\n        return this.#present ? (this.#value as Injected) : undefined;\n    }\n\n    /**\n     * Fills the (owned) slot with the crawler-built default, returning it for convenience. Only valid on an owned,\n     * not-yet-filled slot: borrowed instances are never replaced and an owned slot is filled exactly once (re-setting\n     * would silently orphan the previous instance's lifecycle).","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/owned_or_injected.ts#L52-L88","documentation":"OwnedOrInjected is a slot that either owns a lazily-created default or holds an injected instance. Its `value` getter is strict: if the slot has not been filled yet (neither set() called nor an instance injected), it throws instead of returning undefined. Callers that can tolerate an empty slot must use `maybeValue`.","triggerScenarios":"Reading `.value` on an OwnedOrInjected before initialization — i.e. before the owning component ran set() and before any instance was injected via options/configuration.","commonSituations":"Accessing a crawler sub-client (e.g. a storage client slot) during construction before lazy init; reading `.value` in a getter that runs earlier than the fill logic; forgetting to pass the injected dependency so the owned default was never created.","solutions":["Ensure the owning component's initialization (set()/lazy creation) runs before reading `.value`.","Inject the expected instance explicitly via the options/configuration path that fills the slot.","Use `maybeValue` and handle null when the slot may legitimately be empty.","Move the read after the async initialization point (e.g. after crawler run setup)."],"exampleFix":"// before\nconst client = this.proxyConfigurationSlot.value; // throws if not yet filled\n// after\nconst client = this.proxyConfigurationSlot.maybeValue ?? await this.createDefaultClient();","handlingStrategy":"type-guard","validationCode":"if (slot.maybeValue == null) {\n  throw new Error('Slot not initialized; ensure init ran before use');\n}\nconst client = slot.value;","typeGuard":"function isInitialized<TOwned, TInjected>(slot: OwnedOrInjected<TOwned, TInjected>): boolean {\n  return slot.maybeValue != null;\n}","tryCatchPattern":"let client;\ntry {\n  client = slot.value;\n} catch (err) {\n  if ((err as Error).message === 'OwnedOrInjected value is not initialized yet') {\n    client = slot.maybeValue ?? await initializeDefault();\n  } else throw err;\n}","preventionTips":["Read `maybeValue` when the slot may be empty; reserve `.value` for post-init code.","Ensure initialization (set/inject) happens before any consumer access.","Don't read dependency slots inside constructors; do it in init/run methods.","Inject the dependency explicitly in tests to keep slots filled."],"tags":["initialization","lifecycle","typescript"],"backgroundTag":"value-not-initialized","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}