Hi guys,

Recently an app that my team has been developing has started giving us errors when using the cordova/phonegap plugin called barcodescanner. It was working totally fine up until the end of last week, when it suddenly stopped working and gave us the following error: "Cannot read property 'barcodeScanner' of undefined".

I have tried everything I can think of. I tried recompiling the app, but it didn't work. I tried changing the URL to a tagged version, I tried changing the reference to "identifier" instead of "url", but ditto. I downloaded a sample app which uses the same plugin under the hood, and that one works perfectly, but the one in our app won't even start. I tried replacing the code that one of our colleagues had written with the plugin used in the sample app, and it still doesn't work.

This is what my Extensibility Configurations looks like:

This is what I'm using to check if the plugin is available:


The error message I get is "Cordova plugins are not defined".  I do not understand why this is happening or how to fix it.

Any help would be greatly appreciated.


I am using outsystems service studio 10.0.1011.0

Can you / have you tried republishing the version of the app from last week before it stopped working?

Hi Charise Walraven ,

Despite going in a deep solution, I would recommend you to go with the below quick-fix:

1- Generate a new Build & Install it in Device, If there's an Old build then do remove it first.

2-  Use below instead of if(!!cordova.plugins){} :

    If(cordova)// Your Rest JS here }

    else{    }

or

You can use IsCordovaDefined Action available under Common-Plugins. You can call the IsCordovaDefined  Action before you call your respective Plugin Action. 


3- Update the URL with the latest git version, you can do this by adding #Version at the end of the same URL.

For example:

{     "plugin" :{         "url": "https://github.com/WuglyakBolgoink/cordova-plugin-iroot.git#0.7.0"     } }


4- To check the Plugin Loaded, you can refer the below JS sample:

$parameters.IsLoaded = !!window.cordova && !!IRoot.isRooted;


Hope it Helps,

Assif


Ok, first things first:

- If it was working and it stopped working, something might have changed. Have you tried what Mr. G. have suggested?

- Any specific reason for a custom implementation and not use the supported barcode scanner plugin?


My suggestion:

- I've been stuck in the past with situations like this. The first thing I do is to check the build logs. See if the plugin is retrieved.

- Just do one simple thing: remove the reference to the plugin, create a new app for the plugin, move the plugin module, reference it. After being on the phone for more than a couple of hours, this actually did the trick to someone.


Hope this helps! 

G. Andrew Duthie wrote:

Can you / have you tried republishing the version of the app from last week before it stopped working?

I did do this and it made no difference

assif_tiger wrote:

Hi Charise Walraven ,

Despite going in a deep solution, I would recommend you to go with the below quick-fix:

1- Generate a new Build & Install it in Device, If there's an Old build then do remove it first.

2-  Use below instead of if(!!cordova.plugins){} :

    If(cordova)// Your Rest JS here }

    else{    }

or

You can use IsCordovaDefined Action available under Common-Plugins. You can call the IsCordovaDefined  Action before you call your respective Plugin Action. 


3- Update the URL with the latest git version, you can do this by adding #Version at the end of the same URL.

For example:

{     "plugin" :{         "url": "https://github.com/WuglyakBolgoink/cordova-plugin-iroot.git#0.7.0"     } }


4- To check the Plugin Loaded, you can refer the below JS sample:

$parameters.IsLoaded = !!window.cordova && !!IRoot.isRooted;


Hope it Helps,

Assif



Hey Assif, 


I have been checking using the IsCordovaDefined action, before running the JS checking if the plugins are there. We did use the latest github version, but somehow the plugin wasn't getting loaded usign the github URL or the Identifier.


Thanks,

Charise

Armando Gomes wrote:

Ok, first things first:

- If it was working and it stopped working, something might have changed. Have you tried what Mr. G. have suggested?

- Any specific reason for a custom implementation and not use the supported barcode scanner plugin?


My suggestion:

- I've been stuck in the past with situations like this. The first thing I do is to check the build logs. See if the plugin is retrieved.

- Just do one simple thing: remove the reference to the plugin, create a new app for the plugin, move the plugin module, reference it. After being on the phone for more than a couple of hours, this actually did the trick to someone.


Hope this helps! 

Hey Armando,


Thanks for the tips. The reason we didn't use the barcode scanner plugin was that it doesn't support data matrices.

Hey all, I managed to get this plugin working towards the end of yesterday. I'm still not 100% sure why it's working now as opposed to before, but here's my code for future reference if someone else has the same issue:


/*
 * Checks to see if cordova plugins are defined
 * @returns Boolean
 */
function cordovaPluginsLoaded() {
    return !!cordova.plugins;
}
 
/*
 * Checks to see if barcode scanner plugin is defined
 * @returns Boolean
 */
function barcodeScannerLoaded() {
    return !!cordova.plugins.barcodeScanner;
}

/*
 * Checks to see if barcode scanner and plugins are defined, and sets 
 * ErrorMessage and IsAvailable accordingly
 * @returns Void
 */
function checkPlugin() {
    $parameters.IsAvailable = cordovaPluginsLoaded() && barcodeScannerLoaded();
    if(!$parameters.IsAvailable) {
        if(!cordovaPluginsLoaded()) {
            $parameters.ErrorMessage = "Cordova Plugins not loaded";
        } else {
            $parameters.ErrorMessage = "Barcode Scanner Plugin not loaded";
        }
    }
}

document.addEventListener('deviceready', checkPlugin);
document.removeEventListener('deviceready', checkPlugin);


I think the thing that made the most difference is the event listener.

Nevermind, it just broke again.

One of the "suggested practices" is that you always use a release (<something>.git#<Release>, i.e., #3.2.1) instead of the "bleeding edge" version (<something>.git).

Why are you adding and removing an event listener to check if the plugin is available?