sunnyj58
sunnyj58

Reputation: 103

Unexpected token error when using ionic/cordova

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.

Upvotes: 2

Views: 354

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

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

ScanCode() : any{
    this.barcodeScanner.scan().then((barcodeData) => {
    console.log(barcodeData);
    }, (err) => {
    });
}

Upvotes: 1

Related Questions