Cordova not defined barcode scanner problem [SOLVED]


#1

Hi,

I have a barcode scanner and its suddenly decided to stop working.

I tried to test it on the ionic view app and it is displaying a blank screen. I re downloaded ionic view to see if it was a problem with ionic view but its still not displaying my app.

so I went to the browser to see if there were any errors in the console.

here is the error that is displaying :

ReferenceError: cordova is not defined
at Object.scan (ng-cordova.js:449)
at Scope.$scope.scan (controllers.js:172)
at $parseFunctionCall (ionic.bundle.js:20124)
at ionic.bundle.js:50863
at Scope.$eval (ionic.bundle.js:22178)
at Scope.$apply (ionic.bundle.js:22276)
at HTMLAnchorElement. (ionic.bundle.js:50862)
at HTMLAnchorElement.eventHandler (ionic.bundle.js:10823)
at triggerMouseEvent (ionic.bundle.js:2811)
at tapClick (ionic.bundle.js:2800)

here is my code :

controller.js

.controller(‘PrefCtrl’, function($scope, Loader, FirebaseRef, $rootScope, $location) {
var authData = $rootScope.authData;
$scope.preferences = [];
$scope.pref = {
text: ‘’
};
if (!authData) {
$location.path(’/login’);
return;
}

Loader.showLoading(‘Fetching Preferences…’);

FirebaseRef.child(“users”)
.child(authData.uid)
.on(‘value’, function(snapshot) {

Loader.hideLoading();
$scope.preferences = (snapshot.val() ? snapshot.val().preferences : []) || [];
});
$scope.savePref = function() {
$scope.preferences.push($scope.pref.text);
FirebaseRef
.child(“users”)
.child(authData.uid)
.set({
“preferences”: $scope.preferences
});

$scope.pref.text = ‘’;
};

})

.controller(‘ScanCtrl’, function($scope, Loader, FirebaseRef, $rootScope, $location, $cordovaBarcodeScanner) {

var authData = $rootScope.authData;
var matchedAllergies = [];

if (!authData) {
$location.path(’/login’);
return;
}

Loader.showLoading(‘Fetching Preferences…’);

FirebaseRef
.child(“users”)
.child(authData.uid)
.on(‘value’, function(snapshot) {

    Loader.hideLoading();
    $scope.preferences = (snapshot.val() ? snapshot.val().preferences : []) || [];
});

$scope.scan = function() {
$cordovaBarcodeScanner
.scan()
.then(function(barcodeData) {
// Success! Barcode data is here
$scope.barcodeData = barcodeData;
$scope.barcodeData.text

        //pick the value from barcodeData and
        // barcodeData.text

        var barcode = barcodeData.text;


        // make a call to Firebase to /products and 
        // pass the barcode to get its info
        // ->
        var product = {
            "name" : "Nacho Cheese Sauce - Discovery",
            "barcode": "5018420420419",
            "allergies": ["Peanuts, Sesame seeds, Treenut"],
  "ingredients": "Water, cheddar cheese 15%, cream, red peppers, onion, modified maize starch, olive oil, vegetable bouillon (sugar, potato  starch, calery extract, yeast extract, salt, onion extract, parsley extract, onion powder, turmeric extract, dried parsley, citric acid), concentrated lemon juice, sugar, chilli powder, salt, jalapeño peppers, cumin, garlic purée, garlic powder, garlic extract, black pepper, white pepper, colour (paprika extract), paprika, oregano, acidity regulator (lactic acid), cayenne pepper",
            "suitable to consume" : "Okay for you!, Not for you!"
        };

var product = {
            "name": "nairns - gluten free wholegrain crackers",
            "barcode": "0612322030070",
            "allergies": ["Milk, Nuts, Avenin, gluten"],
            "ingredients": "Wholegrain Oats (86%), Sustainable Palm Fruit Oil, Maize Starch, Sea Salt, Raising Agent (Ammonium bicarbonate), Honey",
  "suitable to consume": "Okay for you!, Not for you!"
        };
    
    var product = {
        "name": "Dairy Milk Whole Nut - Cadbury",
        "barcode": "7622300735951",
        "allergies": ["gluten, nuts,milk"],
        "ingredients": "Milk, sugar, roasted hazelnuts, cocoa butter, cocoa mass, vegetable fats (palm, shea), emulsifiers (E442, E476), flavourings",
        "suitable to consume": "Okay for you!, Not for you!"
    };
    
    var product = {
        "name": "Medium curry - Uncle ben's",
        "barcode": "4002359001758",
        "allergies": ["celery, mustard"],
        "ingredients": "Water, tomatoes, onions (12%), red pepper (5%), cornflour, sugar, coconut (2.8%), lemon juice, roasted onion paste (2%) (onions, Sunflower oil, Salt), Sunflower oil, Spices, Salt, curry powder (0.8%) (contains celery, mustard), ginger, garlic",
         "suitable to consume": "Okay for you!, Not for you!"
    };

        for (var i = 0; i < $scope.preferences.length; i++) {
            for (var j = 0; j < product.allergies.length; j++) {
                if ($scope.preferences[i] === product.allergies[j]) {
                    matchedAllergies.push($scope.preferences[i]);
                }
            };
        };

        // If matchedAllergies.length > 0, he is allergic to
        // matchedAllergies

    }, function(error) {
        // An error occurred
        $scope.error = error;
 });

}
})

index.html

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

tab-scan.html

       <ion-view view-title="Scan">
    <ion-content class="has-subheader padding">
     <a class="button button-positive button-block button-outline icon-left
    ion-qr-scanner" ng-click="scan()">Scan</a> 
       {{error || barcodeData}}
   </ion-content>
 </ion-view>

tab-pref.html

 <ion-view view-title="Preferences">
     <ion-content>
     <h2 class="text-center">My allergies</h2>
        <div class="list">
        <label class="item item-input">
        <input type="text" placeholder="I am allergic to.." ng-model="pref.text">   
        </label>
        <button ng-click="savePref()" class="button button-block button-positive">
        Add Allergy
         </button>
    </div>
<ul class="list" ng-show="preferences">
    <li class="item" ng-repeat="p in preferences">
        {{p}}
    </li>
</ul>
please can someone tell me whats going wrong?

thanks


#2

Just a shot in the dark here but have you tried switching around the ngCordova script and cordova scripts in your index.html?


#3

ive managed to sort it out some files were in the index.html that werent needed

thank you :slight_smile: