{"id":"002eadb2c7fe9b74","repo":"nestjs/nest","slug":"not-initialized-please-call-the-connect-method-002ead","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-redis.ts","lineNumber":220,"sourceCode":"      retryStrategy,\n    };\n  }\n\n  public on<\n    EventKey extends keyof RedisEvents = keyof RedisEvents,\n    EventCallback extends RedisEvents[EventKey] = RedisEvents[EventKey],\n  >(event: EventKey, callback: EventCallback) {\n    if (this.subClient && this.pubClient) {\n      this.subClient.on(event, (...args: [any]) => callback('sub', ...args));\n      this.pubClient.on(event, (...args: [any]) => callback('pub', ...args));\n    } else {\n      this.pendingEventListeners.push({ event, callback });\n    }\n  }\n\n  public unwrap<T>(): T {\n    if (!this.pubClient || !this.subClient) {\n      throw new Error(\n        'Not initialized. Please call the \"connect\" method first.',\n      );\n    }\n    return [this.pubClient, this.subClient] as T;\n  }\n\n  public createRetryStrategy(times: number): undefined | number {\n    if (this.isManuallyClosed) {\n      return undefined;\n    }\n    if (!this.getOptionsProp(this.options, 'retryAttempts')) {\n      this.logger.error(\n        'Redis connection closed and retry attempts not specified',\n      );\n      return;\n    }\n    if (times > this.getOptionsProp(this.options, 'retryAttempts', 0)) {\n      this.logger.error('Retry time exhausted');","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-redis.ts#L202-L238","documentation":"Thrown by ClientRedis.unwrap() when either this.pubClient or this.subClient is null. Redis uses two connections (publisher + subscriber); unwrap() returns [pubClient, subClient] as a tuple. Until connect() creates both, there is nothing to return, so the call is rejected with the generic 'Not initialized' message.","triggerScenarios":"Calling client.unwrap() before client.connect() resolves (both pub and sub clients must be open). Calling unwrap() after close() which destroys both clients. Asymmetric failure where the sub client connected but the pub client did not (or vice versa) — the OR check fails on the missing one.","commonSituations":"onModuleInit lifecycle ordering where unwrap() runs before the Redis connection promise resolves. Reconnect logic that calls close()+unwrap(). Redis broker refusing one of the two connections (auth/firewall) leaving the tuple incomplete.","solutions":["Await client.connect() before client.unwrap().","Verify both Redis connections succeed — check broker auth and network for both pub and sub sockets.","After close(), build a new ClientRedis rather than unwrap()-ing the closed instance.","Subscribe to client.status and only unwrap() after CONNECTED."],"exampleFix":"// before\nconst client = new ClientRedis({ options: {...} });\nconst [pub, sub] = client.unwrap(); // throws\n\n// after\nawait client.connect();\nconst [pub, sub] = client.unwrap<[RedisClient, RedisClient]>();","handlingStrategy":"validation","validationCode":"async function getClients(client: ClientRedis) {\n  await client.connect();\n  return client.unwrap();\n}","typeGuard":"import { ClientRedis } from '@nestjs/microservices';\nconst isRedisClient = (c: unknown): c is ClientRedis => c instanceof ClientRedis;\nfunction isReady(c: ClientRedis): boolean {\n  return !!(c as any).pubClient && !!(c as any).subClient;\n}","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(); Redis needs both pub and sub sockets open.","Verify broker auth/firewall allows both connections.","Recreate the client after close()."],"tags":["redis","client-proxy","lifecycle","typescript"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}