Skip to content

BUG REPORT - Evolution API v2.3.4 - QR Code não sendo gerado - SOLVED TypeError: Cannot read properties of undefined (reading 'state') #2090

@edubortolotti

Description

@edubortolotti

Welcome!

  • Yes, I have searched for similar issues on GitHub and found none.

What did you do?

Environment

  • Evolution API: v2.3.4 (running via npm run build && npm run start, not Dockerized)
  • Baileys: @whiskeysockets/baileys
  • Node.js: 20.x (Linux / Alpine)
  • Environment Variable:
.env
CONFIG_SESSION_PHONE_VERSION=2.3000.1028450369

While initializing a WhatsApp instance (e.g. "Loovree"), the process crashed with:

TypeError: Cannot read properties of undefined (reading 'state')
    at BaileysStartupService.createClient (.../whatsapp.baileys.service.ts:629:40)

Inside the BaileysStartupService.createClient() method, the following snippet was executed:

  creds: this.instance.authState.state.creds,
  keys: makeCacheableSignalKeyStore(this.instance.authState.state.keys, P({ level: 'error' }) as any),
},

However, this.instance.authState was never initialized.
The call to useMultiFileAuthState() (responsible for loading or creating session credentials) was missing, leaving authState as undefined.

Thus, accessing .state caused the fatal error.

What i did to solve:
I inspected the logs and source code around:

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:629,

and confirmed that the authState object was never initialized before being accessed in the createClient() method.

I then patched the code to:

  • Explicitly initialize authState using useMultiFileAuthState() before creating the Baileys socket.
  • Add a guard clause to prevent undefined access to authState.state.

#How?
I placed it before this snippet at the beginning of createClient() in
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:

const fs = require('fs');
const { useMultiFileAuthState } = require('@whiskeysockets/baileys');

const sessionDir = `./session/${this.instance.name || this.instance.id || 'default'}`;
if (!fs.existsSync(sessionDir)) fs.mkdirSync(sessionDir, { recursive: true });

const { state, saveCreds } = await useMultiFileAuthState(sessionDir);
this.instance.authState = { state, saveCreds };

if (!this.instance?.authState?.state) {
  console.error('[WA ERROR] authState not initialized for instance', this.instance?.name || this.instance?.id || 'unknown');
  return;
}

After rebuilding and restarting, the connection initialized successfully, QR generation worked, and the session persisted across restarts.

What did you expect?

Please correct it and save it in Docker. Many users need it to solve their problems.

What did you observe instead of what you expected?

Instead of initializing the WhatsApp client and generating the QR code as expected, the Evolution API process crashed immediately during startup.

The stack trace clearly pointed to BaileysStartupService.createClient() where the property this.instance.authState.state was accessed before being defined:

TypeError: Cannot read properties of undefined (reading 'state')
    at BaileysStartupService.createClient (.../whatsapp.baileys.service.ts:629:40)

In other words, the Baileys socket never got created — the authState object was undefined, so the initialization sequence failed before any QR code or connection event occurred.

The error happened consistently on every attempt to create or reconnect a WhatsApp instance, effectively blocking all sessions from initializing.

Screenshots/Videos

No response

Which version of the API are you using?

I was using Evolution API v2.3.4, executed directly via:

npm run build && npm run start

— without Docker.

The underlying Baileys dependency reported version:

Baileys version: 2.3000.1028457684

What is your environment?

Linux

Other environment specifications

No response

If applicable, paste the log output

No response

Additional Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions