Phonegap-plugin-barcodescanner: ReferenceError: cordova is not defined

Created on 21 Jun 2015  路  12Comments  路  Source: phonegap/phonegap-plugin-barcodescanner

Hi all. A bit of a newbie here to Cordova so please be patient. I am happy to write a getting started guide etc for folks like me if I can get this to work. Thanks in advance.

My code fails in both the browser and my Nexus 4.
My index.html contains this:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>

<script src="lib/jquery.js"></script>
<script src="lib/handlebars.js"></script>
<script src="js/services/memory/EmployeeService.js"></script>
<script src="js/app.js"></script>
<script src="cordova.js"></script>
</body>
</html>

In app.js I have the following:

(function () {
  scan();
    /* ---------------------------------- Local Variables ---------------------------------- */
    var homeTpl = Handlebars.compile($("#home-tpl").html());
    var employeeListTpl = Handlebars.compile($("#employee-list-tpl").html());

    var service = new EmployeeService();
    service.initialize().done(function () {
      renderHomeView();
    });

    /* --------------------------------- Event Registration -------------------------------- */
    //$('.search-key').on('keyup', findByName);
    /*$('.help-btn').on('click', function() {
        alert("Employee Directory v3.4");
    });*/

    document.addEventListener('deviceready', function () {
      StatusBar.overlaysWebView( false );
      StatusBar.backgroundColorByHexString('#ffffff');
      StatusBar.styleDefault();

      if (navigator.notification) { // Override default HTML alert with native dialog
        window.alert = function (message) {
          navigator.notification.alert(
            message,    // message
            null,       // callback
            "Workshop", // title
            'OK'        // buttonName
          );
        };
      }
    }, false);


    /* ---------------------------------- Local Functions ---------------------------------- */
    function findByName() {
      service.findByName($('.search-key').val()).done(function (employees) {
        $('.content').html(employeeListTpl(employees));
      });
    }

    function renderHomeView() {
      $('body').html(homeTpl());
      $('.search-key').on('keyup', findByName);
    }

    /* ---------------------------------- Barcode Scanner ---------------------------------- */
    function scan() {
      alert("Scanning start: ");
      cordova.plugins.barcodeScanner.scan( //<------------- The code breaks at the word cordova
        function (result) {
          alert("We got a barcode\n" +
          "Result: " + result.text + "\n" +
          "Format: " + result.format + "\n" +
          "Cancelled: " + result.cancelled);
        },
        function (error) {
          alert("Scanning failed: " + error);
        }
      );
    }
}());

Most helpful comment

you are loading cordova.js after app.js.
you are calling scan(); from app.js.
your scan function is called, shows an alert() and calls cordova.plugins.barcodeScanner.scan() which breaks because at this point in time no object called "cordova" exists yet

-> load cordova.js before app.js
additionally you should call scan() only after "deviceready" has been fired, not at script load time. hth.

All 12 comments

Same error here. :/

you are loading cordova.js after app.js.
you are calling scan(); from app.js.
your scan function is called, shows an alert() and calls cordova.plugins.barcodeScanner.scan() which breaks because at this point in time no object called "cordova" exists yet

-> load cordova.js before app.js
additionally you should call scan() only after "deviceready" has been fired, not at script load time. hth.

Thanks @rocco, it works now :D

@whirish can you shared, how you fix cordova undefined

@Apranta as I said before:

-> load cordova.js before app.js

additionally you should call scan() only after "deviceready" has been fired, not at script load time.

image

image

still not work :(

A bit more context would be good here. Are you testing on a device (cordova object exists only there)? What's your exact error message, @Apranta ?

i had tested in my device and the error didnt view, and in my laptop only show cordova undefined

well, it's a native function, it will only work on devices

yah but in my device didnt view the error :"

Download the ngCordova plugin and enable it, it's wok for me:
capture

This thread has been automatically locked.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

klickagent picture klickagent  路  3Comments

IYABAO picture IYABAO  路  7Comments

HallmediaDigital picture HallmediaDigital  路  4Comments

brunovinny picture brunovinny  路  6Comments

SunboX picture SunboX  路  9Comments