{"id":"17f6a1b50ab5b68a","repo":"nestjs/nest","slug":"not-initialized-please-call-the-connect-method-17f6a1","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-rmq.ts","lineNumber":328,"sourceCode":"      this._status$.next(RmqStatus.UNBLOCKED);\n      this.logger.log(UNBLOCKED_RMQ_MESSAGE);\n    });\n  }\n\n  public on<\n    EventKey extends keyof RmqEvents = keyof RmqEvents,\n    EventCallback extends RmqEvents[EventKey] = RmqEvents[EventKey],\n  >(event: EventKey, callback: EventCallback) {\n    if (this.client) {\n      this.client.addListener(event, callback);\n    } else {\n      this.pendingEventListeners.push({ event, callback });\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 async handleMessage(\n    packet: unknown,\n    callback: (packet: WritePacket) => any,\n  ): Promise<void>;\n  public async handleMessage(\n    packet: unknown,\n    options: Record<string, unknown>,\n    callback: (packet: WritePacket) => any,\n  ): Promise<void>;\n  public async handleMessage(\n    packet: unknown,\n    options:","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-rmq.ts#L310-L346","documentation":"Thrown by ClientRMQ.unwrap() when this.client is null. this.client is the amqp-connection-manager (AmqpConnectionManager) created during connect(). unwrap() exposes it for native AMQP channel/assertion APIs; before connect() runs there is no connection manager to expose.","triggerScenarios":"Calling client.unwrap() before client.connect() resolves. Calling unwrap() after close() which clears the connection manager. onModuleInit ordering where unwrap() is invoked before the RabbitMQ connection promise settles.","commonSituations":"Asserting queues/exchanges via the raw connection manager before connect() finished. Reconnect routines that close() then unwrap(). RabbitMQ broker unreachable at startup so connect() never completes, but unwrap() is still called downstream.","solutions":["Await client.connect() before client.unwrap().","Confirm the RabbitMQ broker URL, vhost, and credentials allow the connection to complete.","After close(), create a new ClientRMQ rather than unwrap()-ing the closed instance.","Gate unwrap() on the status Observable (RmqStatus.CONNECTED)."],"exampleFix":"// before\nconst client = new ClientRMQ({ options: {...} });\nconst conn = client.unwrap(); // throws\n\n// after\nawait client.connect();\nconst conn = client.unwrap<AmqpConnectionManager>();","handlingStrategy":"validation","validationCode":"async function getRmq(client: ClientRMQ) {\n  await client.connect();\n  return client.unwrap();\n}","typeGuard":"import { ClientRMQ } from '@nestjs/microservices';\nconst isRmqClient = (c: unknown): c is ClientRMQ => c instanceof ClientRMQ;\nfunction isReady(c: ClientRMQ): boolean { return !!(c as any).client; }","tryCatchPattern":"try {\n  return client.unwrap();\n} catch (e) {\n  if (/Not initialized/.test(e?.message)) { await client.connect(); return client.unwrap(); }\n  throw e;\n}","preventionTips":["Await connect() before unwrap().","Confirm RabbitMQ URL/vhost/credentials are valid so connect() completes.","Recreate the client after close()."],"tags":["rabbitmq","amqp","client-proxy","lifecycle","typescript"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}