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
How to solve this? Thanks
Update:
After waiting a few time, the state appear when on: ready.
Here for the detail screenshot.

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.