Paho failure to subscribe

Problem

AMQJS0005E Internal error. Error Message: Cannot read property ‘show’ of undefined

This error is shown sporadically, especially when you Shift-Reload the whole page.

Solution

I have a Vue.js Plugin to show “toast” messages (Vue.toasted). I currently use it for debugging purposes.

Everything worked fine initially, as my code was executed after Vue was initialized and the whole page loaded.

Wanting to accelerate the MQTT subscription, I put in code to launch the subscription earlier.

This is where I ran into this error. As written, it appeared to be a race condition, as it was not consistent.

it turns out that my toasted messages, which I put in for debug, were causing the trouble.

function info(msg){
     console.log(msg);
     /*if (typeof Vue !== ‘undefined’) {
         Vue.toasted.show(msg, {
             action : {
                 text : ‘OK’,
                 onClick : (e, toastObject) => {
                     toastObject.goAway(0);
                 }
             }
         });
     } else {
         console.log(“… Vue not available yet …”);
     }*/   
}

As you see, removing the Vue.toasted.show led to the code performing OK.

Therefore: if PAHO throws error messages, it might be related to some other code you’ve put in, not necessarily to internal Paho state.

Working code:

function info(msg){
     console.log(msg);
     if (typeof Vue !== ‘undefined’ && typeof Vue.toasted !== ‘undefined’) {
         Vue.toasted.show(msg, {
             action : {
                 text : ‘OK’,
                 onClick : (e, toastObject) => {
                     toastObject.goAway(0);
                 }
             }
         });
     } else {
         console.log(“… Vue not available yet …”);
     }
}

This code works, as it also checks whether Vue.toasted has been initialized.