Thursday, 20 October 2022

What should I check? Uncaught (in promise) TypeError: Cannot read property 'MyFunctionName' of undefined URL

The problem seems to be in the distributed exe, but I just don't see any difference, and I don't know what I could check to find out the difference between the 2 machines:

I have added the following script to a webpage:

async function exportChatAsXMLHelper(params){
    let displayname     = params[0];
    let includeMedia    = params[1];
    let debug           = params[2];
    
    await exportChatAsXML(displayname, includeMedia, debug);
}

async function exportChatAsXML(displayname, includeMedia, debug)
{

    let chat = (await WPP.chat.list()).find(m => m.contact && m.contact.name && m.contact.name.includes(displayname));
    await WPP.chat.openChatBottom(chat.id);
    let msgs = await WPP.chat.getMessages(chat.id, {count : -1});

    const log = (obj) => debug && console.log(obj);
    
    log('Total messages: ' + msgs.length);

    let count=msgs.length;

    for (var i = 0; i < count; i++) {
        log('Message number: ' + i);

        let message = msgs[i];
        let xml='';
        xml+= '<message>';
        xml+= '<sender>'+ message.from.user +'</sender>';
        xml+= '<receiver>'+ message.to.user +'</receiver>';

        xml+= '<type>'+ (message.type || '') +'</type>';

        if(message.type == 'chat')
        {
            xml+= '<body>'+ message.body +'</body>';
        }

        if(message.type != 'chat' && includeMedia) 
        {
            xml+= '<media>';
            xml+= '<caption>'+ (message.caption || '') +'</caption>';
            xml+= '<filename>'+ (message.filename || '') +'</filename>';

            log('Downloading media');

            try
            {
                let mediabody = await mediatoBase64(message.id);
                xml+= '<MediaDownloadStatus>success</MediaDownloadStatus>';
                xml+= '<base64>'+ mediabody +'</base64>';
            }
            catch(e)
            {
                xml+= '<base64></base64>';
                xml+= '<MediaDownloadStatus>fail</MediaDownloadStatus>';
            }
            xml+= '</media>';
        }

        xml+= '</message>';

        alert('before'); //this is still shown

        //where is JSCallbackReceiver defined? it is from vb6
       window.JSCallbackReceiver.OnWhatsAppXMLReceived(i, count, xml);
       
       alert('after'); //this is not shown

       xml='';
    }
}


//-----
async function mediatoBase64(msgid) {
    let blob = await WPP.chat.downloadMedia(msgid);
    return new Promise((resolve, _) => {
        const reader = new FileReader();
        reader.onloadend = () => resolve(reader.result);
        reader.readAsDataURL(blob);
    });
}

It works fine on my developer's machine, but throws an error on a client's machine:

Uncaught (in promise) TypeError: Cannot read property 'OnWhatsAppXMLReceived' of undefined https://web.whatsapp.com/ 80

I have added some alerts now to see where exactely it goes wrong. The alert "before" is shown, the alert "after" is not shown, so the error definitively occurs in

   window.JSCallbackReceiver.OnWhatsAppXMLReceived(i, count, xml);
   alert('after');

What could I check to see what exactely goes wrong / what is different on the client's machine?

The callback object is defined in VB6 like this:

Private WithEvents m_JSCallback As clsJSCallbackReceiver

Set m_JSCallback = New clsJSCallbackReceiver
Me.webkit1.AddObject "JSCallbackReceiver", m_JSCallback

Does the error message mean that the browser does not find the object that I added via AddObject?

I am using mobileFx webkit browser which is based on Chromium, but I guess that is not important.

This is what the class clsJSCallbackReceiver looks like in VB6:

Option Explicit

Public Event WhatsAppXMLReceived(ByVal Index As Long, ByVal Count As Long, ByVal xml As String)

Public Sub OnWhatsAppXMLReceived(ByVal uIndex As Long, ByVal uCount As Long, ByVal uXml As String)

    RaiseEvent WhatsAppXMLReceived(uIndex, uCount, uXml)
    
 End Sub

Thank you!



from What should I check? Uncaught (in promise) TypeError: Cannot read property 'MyFunctionName' of undefined URL

No comments:

Post a Comment