Hello Ionic World! I’m just getting into mobile development, and you guys have really helped me get started quick! Those docs and the host of guides on youtube are great!
That being said, I have hit a bit of a snag with my first app.
I am attempting to make an asset tracker that makes use of the Ionic Native BarcodeScanner plugin for scanning asset tags. (Aside: I eventually want to link these codes to a database, but I will save that question and it’s subqueries for a separate post in the near future)
I have done my best to link the scan functionality to a button, but whenever I click it in the ionic lab web simulator, it gives me the following error:
TypeError: Unable to get property 'scan' of undefined or null reference
at HomePage.prototype.ScanBarcode (http://localhost:8100/build/main.js:55816:9)
at Anonymous function (Function code:236:9)
at handleEvent (http://localhost:8100/build/main.js:12192:87)
at callWithDebugContext (http://localhost:8100/build/main.js:13484:9)
at debugHandleEvent (http://localhost:8100/build/main.js:13072:5)
at dispatchEvent (http://localhost:8100/build/main.js:9092:5)
at Anonymous function (http://localhost:8100/build/main.js:9684:13)
at Anonymous function (http://localhost:8100/build/main.js:33436:9)
at t.prototype.invokeTask (http://localhost:8100/build/polyfills.js:3:9843)
at onInvokeTask (http://localhost:8100/build/main.js:4418:21)
When using the Ionic View app, it does not display an error, but does refrain from doing anything useful when I tap the button.
I will paste my code below for your perusal, and I hope to have a reply soon. This app needs to be at least semi-functional by this coming Friday, or I may miss out on an opportunity I really don’t want to lose.
HTML:
<ion-header>
<ion-navbar color="primary">
<ion-title text-center>
SVUIT Asset Tracker App
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<h2>Click Below to Scan an Asset Tag</h2>
<!-- Item with a label and content -->
<ion-item>
<ion-label>Last Scan:</ion-label>
<div item-content>
{{ barcodeData }}
</div>
</ion-item>
<!-- Bind the click event to a method -->
<button ion-button block color="dark" (click)="ScanBarcode()">
Scan Barcode
</button>
</ion-content>
TS
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
barcodeScanner: BarcodeScanner;
constructor(public navCtrl: NavController) {
}
barcodeData = '';
ScanBarcode() {
this.barcodeScanner.scan().then((barcodeData) => {
// Success! Barcode data is here
console.log("Barcode Scan Successful: " + barcodeData);
}, (err) => {
// An error occurred
console.error("Scan Unsuccessful: " + err);
});
}
}
Thanks guys! Hope you can help me out soon! This is my very first app, and my first experience with Javascript! I’m eager to learn, so any friendly advice is appreciated!
PS- it’s also my first time posting my programming woes on a forum of any kind, so if I’ve done anything unspeakably taboo, please forgive me and tell me how I’ve sinned.