cordova ionic barcode scanner error

4.1k views Asked by At

i am getting same error

barcodescanner.js:130 Uncaught ReferenceError: require is not defined

ionic.bundle.js:20306 ReferenceError: cordova is not defined

Cordova CLI: 5.0.0 Gulp version: CLI version 3.8.11 Gulp local: Local version 3.8.11 Ionic Version: 1.0.0 Ionic CLI Version: 1.4.3 Ionic App Lib Version: 0.0.19 ios-deploy version: Not installed ios-sim version: 3.1.1 OS: Mac OS X Mavericks Node Version: v0.12.2 Xcode version: Xcode 6.1.1 Build version 6A2008a

my index.html

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>


<script src="js/ng-cordova.min.js"></script>
<script src="js/ng-cordova-mocks.min.js"></script>
<script src="js/barcodescanner.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>

------app.js contains-----------------

angular.module('starter', ['ionic', 'starter.controllers', 'ngCordova'])

------controllers.js contains-------------------

angular.module('starter.controllers', ['ngCordova'])

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $stateParams, $http, $cordovaBarcodeScanner) {

.......

$scope.scanBarcode = function() {
var scanner = cordova.plugins.BarcodeScanner;
if (scanner == undefined) {
scanner = cordova.require("cordova/plugin/BarcodeScanner");
}

scanner.scan()
    
        .then(function (imageData) {
    
                alert(imageData.text);
                console.log(imageData.format);
    
                // Success! Barcode data is here
    
             }, function (err) {
    
                 console.log(err);
    
                 // An error occurred
    
             });
    
    };

can anybody help me out ?

1

There are 1 answers

2
manzapanza On BEST ANSWER

If you are using the iOS Simulator you can't test this plugin because the simulator does not support the camera. So you need to use a real device to test you app.

Check the documentation: Testing and Debugging in iOS Simulator

UPDATE

Your code seems to have some errors. Try this example, I haven't tested but should works:

Controller

angular.module('starter.controllers', ['ngCordova'])

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $stateParams, $http, $cordovaBarcodeScanner) {

  $scope.scanBarcode = function(){
    $cordovaBarcodeScanner.scan().then(function(barcodeData) {
      // Success! Barcode data is here
      alert('barcode scanned:' +  barcodeData.text);
    }, function(error) {
      alert('Error')
      console.log(error);
      // An error occurred
    });      
  };

});

HTML:

<ion-content>
  <ion-list>
    <ion-item ng-click="scanBarcode()">
      Scan Barcode
    </ion-item>
  </ion-list>
</ion-content>