pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.05k stars 3.58k forks source link

Cannot Read Property of undefined #2831

Closed sparrow-code closed 5 months ago

sparrow-code commented 5 months ago

Is there an existing issue for this?

Describe the bug

i have install latest version of this package

whatsapp-web.js/webCache/LocalWebCache.js

this file on function

async persist(indexHtml) { // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9) const version = indexHtml.match(/manifest-([\d\.]+).json/)[1]; if(!version) return;

    it work fine until i didn't scan mine QR code

and server is not restarted

but if server have been restarted then i got errror

C:\Users\SDWORLLD\Documents\Project\whatsappBot\a2zv1\node_modules\whatsapp-web.js\src\webCache\LocalWebCache.js:36   
        const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];

TypeError: Cannot read properties of null (reading '1')    
    at LocalWebCache.persist (\node_modules\whatsapp-web.js\src\webCache\LocalWebCache.js:36:69)
    at \node_modules\whatsapp-web.js\src\Client.js:745:36        
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

so i modify mine code on line

async initWebVersionCache() {
        const { type: webCacheType, ...webCacheOptions } = this.options.webVersionCache;
        const webCache = WebCacheFactory.createWebCache(webCacheType, webCacheOptions);

        const requestedVersion = this.options.webVersion;
        const versionContent = await webCache.resolve(requestedVersion);

        if(versionContent) {
            await this.pupPage.setRequestInterception(true);
            this.pupPage.on('request', async (req) => {
                if(req.url() === WhatsWebURL) {
                    req.respond({
                        status: 200,
                        contentType: 'text/html',
                        body: versionContent
                    }); 
                } else {
                    req.continue();
                }
            });
        } else {
            this.pupPage.on('response', async (res) => {
                if(res.ok() && res.url() === WhatsWebURL) {
                    // await webCache.persist(await res.text());
                    await webCache.persist(await res.text(), requestedVersion);
                }
            });
        }
    }

but another and so manny error arrived again

i have used this strategy to authenticate it

const client = new Client({ authStrategy: new LocalAuth(), });

Expected behavior

While i restart mine code it should login/authenticate normally and work simpally

Steps to Reproduce the Bug or Issue

1 authenticate with this starategy

const client = new Client({ authStrategy: new LocalAuth(), });

then restart server again

Relevant Code

async initWebVersionCache() {
        const { type: webCacheType, ...webCacheOptions } = this.options.webVersionCache;
        const webCache = WebCacheFactory.createWebCache(webCacheType, webCacheOptions);

        const requestedVersion = this.options.webVersion;
        const versionContent = await webCache.resolve(requestedVersion);

        if(versionContent) {
            await this.pupPage.setRequestInterception(true);
            this.pupPage.on('request', async (req) => {
                if(req.url() === WhatsWebURL) {
                    req.respond({
                        status: 200,
                        contentType: 'text/html',
                        body: versionContent
                    }); 
                } else {
                    req.continue();
                }
            });
        } else {
            this.pupPage.on('response', async (res) => {
                if(res.ok() && res.url() === WhatsWebURL) {
                    // await webCache.persist(await res.text());
                    await webCache.persist(await res.text());
                }
            });
        }
    }

async initWebVersionCache() {
        const { type: webCacheType, ...webCacheOptions } = this.options.webVersionCache;
        const webCache = WebCacheFactory.createWebCache(webCacheType, webCacheOptions);

        const requestedVersion = this.options.webVersion;
        const versionContent = await webCache.resolve(requestedVersion);

        if(versionContent) {
            await this.pupPage.setRequestInterception(true);
            this.pupPage.on('request', async (req) => {
                if(req.url() === WhatsWebURL) {
                    req.respond({
                        status: 200,
                        contentType: 'text/html',
                        body: versionContent
                    }); 
                } else {
                    req.continue();
                }
            });
        } else {
            this.pupPage.on('response', async (res) => {
                if(res.ok() && res.url() === WhatsWebURL) {
                    // await webCache.persist(await res.text());
                    await webCache.persist(await res.text());
                }
            });
        }
    }

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS - Windows Phone - Android whatsapp-web.js version- 1.23.0 Whatsapp Web Version - 2.2411.2 Node Js Version - v20.10.0

Additional context

No i think i have explain and tell everything, and i have try to fix it by mine self

alechkos commented 5 months ago

Related to #2789