{"id":"9a4842a910c93c96","repo":"nestjs/nest","slug":"no-consumer-initialized-please-call-the-connect","errorCode":null,"errorMessage":"No consumer initialized. Please, call the \"connect\" method first.","messagePattern":"No consumer initialized\\. Please, call the \"connect\" method first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/client/client-kafka.ts","lineNumber":77,"sourceCode":"  extends ClientProxy<never, KafkaStatus>\n  implements ClientKafkaProxy\n{\n  protected logger = new Logger(ClientKafka.name);\n  protected client: Kafka | null = null;\n  protected parser: KafkaParser | null = null;\n  protected initialized: Promise<void> | null = null;\n  protected responsePatterns: string[] = [];\n  protected consumerAssignments: { [key: string]: number } = {};\n  protected brokers: string[] | BrokersFunction;\n  protected clientId: string;\n  protected groupId: string;\n  protected producerOnlyMode: boolean;\n  protected _consumer: Consumer | null = null;\n  protected _producer: Producer | null = null;\n\n  get consumer(): Consumer {\n    if (!this._consumer) {\n      throw new Error(\n        'No consumer initialized. Please, call the \"connect\" method first.',\n      );\n    }\n    return this._consumer;\n  }\n\n  get producer(): Producer {\n    if (!this._producer) {\n      throw new Error(\n        'No producer initialized. Please, call the \"connect\" method first.',\n      );\n    }\n    return this._producer;\n  }\n\n  constructor(protected readonly options: Required<KafkaOptions>['options']) {\n    super();\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-kafka.ts#L59-L95","documentation":"The `ClientKafka.consumer` getter throws when `_consumer` is null — i.e. before `connect()` has resolved, after `close()`, or whenever the client is running in `producerOnlyMode` (where no consumer is allocated). Guards any consumer-side API access.","triggerScenarios":"Accessing `clientKafka.consumer` before awaiting `connect()`; accessing it in `producerOnlyMode: true`; accessing it after `close()`.","commonSituations":"Forgetting to `await client.connect()` in `onModuleInit`; calling `commitOffsets`/`subscribeToResponseOf` flow in producer-only mode; race where access precedes connect resolution.","solutions":["Await `connect()` before using the consumer (or rely on the framework's `@Client` auto-connect via `onModuleInit`).","If you only produce, set `producerOnlyMode: true` and stop accessing `consumer`.","After `close()`, call `connect()` again before reuse."],"exampleFix":"// before\nasync onModuleInit() {\n  this.client.subscribeToResponseOf('user.created');\n  const c = this.client.consumer; // throws\n}\n// after\nasync onModuleInit() {\n  this.client.subscribeToResponseOf('user.created');\n  await this.client.connect();\n  const c = this.client.consumer; // ok\n}","handlingStrategy":"validation","validationCode":"// Ensure connect() resolved before touching the consumer\nawait this.client.connect();\nconst assignments = this.client.getConsumerAssignments();\nif (Object.keys(assignments).length === 0) { /* consumer not ready */ }","typeGuard":"function hasConsumer(c: ClientKafka): boolean { return Reflect.get(c, '_consumer') != null; }","tryCatchPattern":null,"preventionTips":["Always `await client.connect()` in `onModuleInit` (or rely on framework auto-connect).","Don't access `consumer` when `producerOnlyMode: true`.","Call `connect()` again after `close()` before reusing the client."],"tags":["kafka","microservices","lifecycle","client-proxy","nestjs"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}