{"id":"ce2ea74efb34ce58","repo":"nestjs/nest","slug":"not-initialized-please-call-the-connect-method","errorCode":null,"errorMessage":"Not initialized. Please call the \"connect\" method first.","messagePattern":"Not initialized\\. Please call the \"connect\" method first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/client/client-kafka.ts","lineNumber":285,"sourceCode":"      resetOnDisconnect: false,\n    });\n    connectableSource.connect();\n    return connectableSource;\n  }\n\n  public commitOffsets(\n    topicPartitions: TopicPartitionOffsetAndMetadata[],\n  ): Promise<void> {\n    if (this._consumer) {\n      return this._consumer.commitOffsets(topicPartitions);\n    } else {\n      throw new Error('No consumer initialized');\n    }\n  }\n\n  public unwrap<T>(): T {\n    if (!this.client) {\n      throw new Error(\n        'Not initialized. Please call the \"connect\" method first.',\n      );\n    }\n    return this.client as T;\n  }\n\n  public on<\n    EventKey extends string | number | symbol = string | number | symbol,\n    EventCallback = any,\n  >(event: EventKey, callback: EventCallback) {\n    throw new Error('Method is not supported for Kafka client');\n  }\n\n  protected registerConsumerEventListeners() {\n    if (!this._consumer) {\n      return;\n    }\n    this._consumer.on(this._consumer.events.CONNECT, () =>","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-kafka.ts#L267-L303","documentation":"`ClientKafka.unwrap()` throws when `this.client` (the native kafkajs `Kafka` instance) is null, i.e. before `connect()` has been called. Once connected, `unwrap()` returns the underlying kafkajs instance for advanced use cases not covered by the NestJS wrapper.","triggerScenarios":"Calling `clientKafka.unwrap()` before `connect()` has run (e.g. synchronously right after construction, or in `onModuleInit` before the connect await).","commonSituations":"Needing the raw kafkajs `Kafka`/admin/producer object for advanced APIs; calling unwrap too early in the lifecycle; after `close()` reset the client to null.","solutions":["Await `connect()` before calling `unwrap()`.","Cache the result of `unwrap()` after connect if you need it repeatedly.","Prefer the wrapped APIs (`subscribeToResponseOf`, `emit`, `send`) when possible."],"exampleFix":"// before\nconst kafka = this.client.unwrap(); // throws\n// after\nasync onModuleInit() {\n  await this.client.connect();\n  const kafka = this.client.unwrap();\n  const admin = kafka.admin();\n}","handlingStrategy":"validation","validationCode":"// Connect before unwrapping the native kafkajs client\nawait this.client.connect();\nconst kafka = this.client.unwrap();","typeGuard":"function hasKafkaClient(c: ClientKafka): boolean { return Reflect.get(c, 'client') != null; }","tryCatchPattern":"try {\n  return this.client.unwrap();\n} catch (e) {\n  if (e instanceof Error && /Not initialized/.test(e.message)) { await this.client.connect(); return this.client.unwrap(); }\n  throw e;\n}","preventionTips":["Await `connect()` before calling `unwrap()`.","Cache the unwrapped instance after connect if used repeatedly.","Prefer the wrapped APIs unless you need kafkajs-specific features."],"tags":["kafka","microservices","lifecycle","api-misuse","nestjs"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}