2

I am following trying to set up a barcode scanner using the following - https://ionicframework.com/docs/native/barcode-scanner/

This is my code -

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 {

constructor(private barcodeScanner: BarcodeScanner) { 

}

this.barcodeScanner.scan().then((barcodeData) => {

console.log(barcodeData);

}, (err) => {

});
}

When I run it, I get an error saying

Typescript Error
Unexpected token. A constructor, method, accessor, or property was expected.

I am not sure what is causing this error, as I have followed the code given.

1 Answer 1

1

You need to place the code inside a constructor or a function.

ScanCode() : any{
    this.barcodeScanner.scan().then((barcodeData) => {
    console.log(barcodeData);
    }, (err) => {
    });
}
Sign up to request clarification or add additional context in comments.

7 Comments

Ah thank you. The errors are gone, but now when I run the code nothing happens. I set a button up in the view - <button ion-button (click)="ScanCode()">Scan Barcode</button> When I click on the button, nothing happens. Nothing runs etc. Any ideas?
you need to call the function ScanCode() from your template wherever its needed. mark if the answer helped
I think you replied before my edit. I tried that, no errors now. But nothing happens, camera doesnt open etc
you need to add a debugger and see what happens
Is this where I need to use an emulator? I have had so many issues. Ive installed android studio, but AVD hasnt installed. Ive tried uninstalling and reinstalling but no success. When I run - ionic cordova emulate android I get an error telling me to run appdata\local\android\sdk\tools\android.bat to create AVDs, but I click this and a console window flicks on and off
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.