Whatsapp-web.js: Unable to use getState

Created on 5 Oct 2020  路  5Comments  路  Source: pedroslopez/whatsapp-web.js

Just trying to use client.getState() and getting this error:

UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'AppState' of undefined

This is what my code look like:

const whatsapp = new Client({ 
  session: sessionCfg,
  restartOnAuthFail: true,
  puppeteer: {
    headless: true,
    args: ['--no-sandbox']
  } 
});

whatsapp.initialize();

whatsapp.getState().then((data) => { 
  console.log(data)
});

Library

  • NodeJS version: v12.10.0
  • npm version: 6.14.8
  • whatsapp-web.js version: ^1.8.2

How to solve this? Thanks

bug

All 5 comments

Update:
After waiting a few time, the state appear when on: ready.

Here for the detail screenshot.
Unable to use getState

Sorry, I can't understand what actually happend.

You run the getState method before the client is ready. So try it this way;

const whatsapp = new Client({ 
  session: sessionCfg,
  restartOnAuthFail: true,
  puppeteer: {
    headless: true,
    args: ['--no-sandbox']
  } 
});

whatsapp.on('ready', async () => {
    console.log('Client is ready!');
    whatsapp.getState().then((result) => { 
        if(!result.match("CONNECTED")) 
            throw Error("Whatsapp Client not connected")
        else 
            console.log("Whatsapp Client State =", result)
    });
});

whatsapp.initialize();


Thanks, so I must call the getState when the client is ready, right?

Thanks, so I must call the getState when the client is ready, right?

Yes, it makes more sense to do it when the client is ready. Actually, you should do a lot of things when the client is ready. Therefore, by creating a middleware, you can find out if the client is ready and perform your operations flawlessly. 馃憤

Okay, thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

riagie picture riagie  路  6Comments

fdciabdul picture fdciabdul  路  6Comments

therevolt picture therevolt  路  4Comments

gabriel-tandil picture gabriel-tandil  路  5Comments

andersondeoliveiramachado picture andersondeoliveiramachado  路  7Comments