Search code examples
androidcordovabarcode-scannerngcordova

ngCordova barcode scanner is providing Object Object in the scannedData


I am new to Cordova. I am trying to scan a QR code by referring to this document. When I do that in alert, it is providing [object Object] in scanned data. Anyone have idea on this.

document.addEventListener("deviceready", function () {
$cordovaBarcodeScanner
  .scan()
  .then(function(barcodeData) {
    console.log(barcodeData);
    alert(barcodeData);
  }, function(error) {
    console.log(error);
  });

   $cordovaBarcodeScanner
  .encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com")
  .then(function(success) {
    alert(success);
  }, function(error) {
    // An error occurred
  });

   },false);

Can anyone tell me what is the use of encode here?


Solution

  • When you you get a scanned bar code (or qr code) data in .then block, you get an object (hash) of data. If you want to see it in the alert box then you have to stringify it:

    alert(JSON.stringify(barcodeData));
    

    Since $cordovaBarcodeScanner plugin works only on physical devise, you don't want to use console.log.

    Regarding .encode method. It is currently not supported. So, there is no point of using it. The documentation also mentions this fact. So, just remove that part of your code:

    document.addEventListener("deviceready", function () {
      $cordovaBarcodeScanner
        .scan()
        .then(function(barcodeData) {
          alert(JSON.stringify(barcodeData));
        }, function(error) {
          alert(JSON.stringify(error));
        });
    }, false);