Need help in $cordovaPrinter - can't make it work in my app


#1

I followed the tutorial from thissite. I was able to make it work when I did a POC. I then tried to add the controller to my app but I keep getting this error: Cannot read property ‘printer’ of undefined.

Here is my controller:

transactionController.controller("CreateInvoiceController", ["$scope", "$cordovaPrinter", "$ionicPlatform", function($scope, $cordovaPrinter, $ionicPlatform) {$scope.orderInvoiceHeader = '<head><meta charset="utf-8"><title></title><link href="lib/ionic/css/ionic.css" rel="stylesheet"><link href="css/style.css" rel="stylesheet" media="print"><script src="lib/ionic/js/ionic.bundle.js"></script></head>';
$scope.items = [{productkey:16, productname: "Coca cola in can", category: "Beverage", price: 50, quantity: 7, unitPrice: 50},
               {productkey:17, productname: "Sprite in can", category: "Beverage", price: 50, quantity: 5, unitPrice: 50},
               {productkey:18, productname: "Steamed Rice", category: "Sides", price: 35, quantity: 4, unitPrice: 40}];
$scope.header = {amountProduct: 645, amountService: 50, discountAmount: 105, orderdateunix: 1491408005, orderno: "36", server:"Waiter", tableNo: "10", taxAmount: 50, totalAmount: 640, customername: "Lastname, Firstname"}
$scope.itemList = '';
$scope.category = '';
// Add identifiers
$scope.orderIdentifier1 = "Table No.";
$scope.orderIdentifier2 = "Server";

// Add Business details
$scope.businessid1 = '<p>Good Eats Coffeeshop</p>';
$scope.businessid2 = '<p>Georgia</p>';
$scope.businessid3 = '<p>USA</p>';
// Add items to invoice
for (i = 0; i < $scope.items.length; i++) {
    // If category change, display category
    if ($scope.category != $scope.items[i].category) {
        // save category detail
        $scope.category = $scope.items[i].category;
        $scope.itemList = $scope.itemList + '<div class="row"><h5><p>Category:' + $scope.items[i].category + '</p></h5></div>';
    }
    $scope.itemList = $scope.itemList + '<div class="row"><div class="col col-20">' + $scope.items[i].productname + '</div><div class="col col-10">' + $scope.items[i].quantity + ' @ ' + $scope.items[i].unitPrice + '</div><div class="col" align="right">' + ($scope.items[i].quantity * $scope.items[i].unitPrice) + '</div></div>' 
    
}
// Add one row before totals
$scope.itemList = $scope.itemList + '<div class="row"></div>'
// Add Product totals
$scope.itemList = $scope.itemList + '<div class="row"><div class="col col-80" align="right" >Sub Total:</div><div class="col" align="right">' + $scope.header.amountProduct + '</div></div>'
// Add Tax Detail
$scope.itemList = $scope.itemList + '<div class="row"><div class="col col-80" align="right" >Tax Amount:</div><div class="col" align="right"> ' + $scope.header.taxAmount + '</div></div>'
// Add Service Charge
$scope.itemList = $scope.itemList + '<div class="row"><div class="col col-80" align="right" >Service Charge: </div><div class="col" align="right">' + $scope.header.amountService + '</div></div>'
// Add Discount Amount Detail
$scope.itemList = $scope.itemList + '<div class="row"><div class="col col-80" align="right" >less Discount: </div><div class="col" align="right">' + $scope.header.discountAmount + '</div></div>'
// Show Total Amount
$scope.itemList = $scope.itemList + '<div class="row"><strong><div class="col col-80" align="right" >Total Amount: </div><div class="col" align="right">' + $scope.header.totalAmount + '</div></strong></div>'

$scope.htmlToPrint = '<html>' + $scope.orderInvoiceHeader + '<body><h3>' + $scope.businessid1 + $scope.businessid2 + $scope.businessid3 + '</h3><h4><p>Order No: ' + $scope.header.orderno +'</p><p>' + $scope.orderIdentifier1 + ': '+ $scope.header.tableNo + '</p><p>' + $scope.orderIdentifier2 + ':' + $scope.header.server + '</p><p>Customer: ' + $scope.header.customername + '</p></h4>' + $scope.itemList + '</body></html>';
    $ionicPlatform.ready(function() {

        $scope.print = function() {
            if($cordovaPrinter.isAvailable()) {
                $cordovaPrinter.print($scope.htmlToPrint);
            } else {
                alert("Printing is not available on device");
            }
        }    }); }]);   

I am at a loss on how to fix this problem. Any suggestions on how to debug this?


#2

Nothing in your code uses a property printer - so this error can not really be connected to the code you posted.


#3

when I inspect the code, it points to the following:

at Object.isAvailable (ng-cordova.min.js:9)
at Scope.$scope.print (transaction.js:2661) 

the problem occurs at this line when I debug my code:

            if($cordovaPrinter.isAvailable()) {

#4

Ok, then it is probably this:

Did you install the plugin successfully?


#5

i tried to re-install the plugin and it seems to be working now. thanks for pointing me to the right direction. (i spent a couple of hours trying to make this work)


#6

Well, happens :wink:

Good luck.