0
votes

I am developing an application to read codes that I am using the BarcodeScanner but when I execute the application on my device I get the following error

**

**Error running it on my iphone 6 using ionic DevApp Error Runtime Error Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])is not a function. (In 'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])(this, "scan", {"callbackOrder":"reverse"}, arguments)', 'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])'is an instance of Object)

Error that is displayed when executing it in ionic serve, ERROR TypeError: Object(...) is not a function at BarcodeScanner.scan (index.js:31) at MenuPage.webpackJsonp.101.MenuPage.scanQR (menu.ts:53) at Object.eval [as handleEvent] (MenuPage.html:17) at handleEvent (core.js:13589) at callWithDebugContext (core.js:15098) at Object.debugHandleEvent [as handleEvent] (core.js:14685) at dispatchEvent (core.js:10004) at core.js:10629 at HTMLButtonElement. (platform-browser.js:2628) at t.invokeTask (polyfills.js:3) **

at the beginning I had the error that my app-module.ts did not recognize me the BarcodeScanner because when calling it in providers me, TS2322: Type 'BarcodeScannerOriginal' is not assignable to type 'Provider'. Type 'BarcodeScannerOriginal' is not assignable to type 'FactoryProvider'. Property 'provide' is missing in type 'BarcodeScannerOriginal'

** Menu.html

<ion-header class="toolbar">
  <ion-navbar>
    <ion-title>Scan</ion-title>
  </ion-navbar>

</ion-header>

<ion-content padding class="Scan">
  <div class="row">
    <div class="col">
      <h2>Scan your QR Code Here</h2>
    </div>
    <div class="col">
      <h3>{{eventTitle}}</h3>
    </div>
  </div>
  <button ion-button block color="secondary" class="Scan-button" (click)="scanQR()" [disabled]="loading">{{buttonText}}</button>
</ion-content>

Menu.ts

import { Component } from '@angular/core';
import {Platform} from "ionic-angular";
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { ToastController } from 'ionic-angular';
import { BarcodeScanner, BarcodeScannerOptions } from '@ionic-native/barcode-scanner/ngx';


/**
 * Generated class for the MenuPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-menu',
  templateUrl: 'menu.html',
})
export class MenuPage {

  public scannedText: string;
  public buttonText: string;
  public loading: boolean;
  private eventId: number;
  public eventTitle: string;

  num: string;

  // @ts-ignore
  constructor(private _nav: NavController,
              private _navParams: NavParams,
              private _barcodeScanner: BarcodeScanner) {



  }
  ionViewDidLoad() {
    this.eventId = this._navParams.get('eventId');
    this.eventTitle = this._navParams.get('eventTitle');

    this.buttonText = "Scan";
    this.loading = false;
  }

  public scanQR() {
    this.buttonText = "Loading..";
    this.loading = true;

    this._barcodeScanner.scan().then((barcodeData) => {
      if (barcodeData.cancelled) {
        console.log("User cancelled the action!");
        this.buttonText = "Scan";
        this.loading = false;
        return false;
      }
      console.log("Scanned successfully!");
      console.log(barcodeData);
      this.goToResult(barcodeData);
    }, (err) => {
      console.log(err);
    });
  }

  private goToResult(barcodeData) {
    this._nav.push(ScanResultPage, {
      scannedText: barcodeData.text
    });
  }
}

app-module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import {MenuPage} from "../pages/menu/menu";
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { HttpClientModule } from '@angular/common/http';

import { FormsModule } from '@angular/forms';
import {HttpModule} from "@angular/http";


// @ts-ignore
@NgModule({
  declarations: [
    MyApp,
    HomePage,
    MenuPage
  ],
  imports: [
    BrowserModule, HttpClientModule,
    IonicModule.forRoot(MyApp),
    HttpModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    MenuPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    BarcodeScanner,
    {provide: ErrorHandler, useClass: IonicErrorHandler}




  ]


})
export class AppModule {}

enter image description here

1
It's unclear what the exact error is that you are getting. Maybe edit this question and make it clear exactly what message you are getting.user1531971
The error that I receive when doing the action, click on the Scan button, it is the one I show in the attached image. Friend does not show me any other error and that is why I am deconcentrated.VICTOR CETZ
Few people are going to visit an external image link, especially for error messages, which should be in the question as searchable, copyable text. Fix the formatting of this question so it is clear what is output and what is explanation. Provide the full error message or stacktrace. Show your research. This is all explained in How to Ask.user1531971
Error running it on my iphone 6 using ionic DevApp Error Runtime Error Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])is not a function. (In 'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])(this, "scan", {"callbackOrder":"reverse"}, arguments)', 'Object(_WEBPACK_IMPORTED_MODULE_1_ionic_native_core_["cordova"])'is an instance of Object)VICTOR CETZ
Error that is displayed when executing it in ionic serve, ERROR TypeError: Object(...) is not a function at BarcodeScanner.scan (index.js:31) at MenuPage.webpackJsonp.101.MenuPage.scanQR (menu.ts:53) at Object.eval [as handleEvent] (MenuPage.html:17) at handleEvent (core.js:13589) at callWithDebugContext (core.js:15098) at Object.debugHandleEvent [as handleEvent] (core.js:14685) at dispatchEvent (core.js:10004) at core.js:10629 at HTMLButtonElement.<anonymous> (platform-browser.js:2628) at t.invokeTask (polyfills.js:3)VICTOR CETZ

1 Answers

1
votes

My guess is you are on Ionic 3 but you are using the native plugin version supported for Ionic 4.

Solution

Uninstall both cordova and ionic native plugin

$ionic cordova plugin remove phonegap-plugin-barcodescanner
$npm uninstall @ionic-native/barcode-scanner

Install version 4.

$ ionic cordova plugin add phonegap-plugin-barcodescanner
$ npm install --save @ionic-native/barcode-scanner@4

And don't append ngx at the end of the import.

import { BarcodeScanner } from '@ionic-native/barcode-scanner';

Note

If you are using Ionic 3, try following the Ionic v3 guide instead of the latest guide

Ionic v3 guide:https://ionicframework.com/docs/v3/native/barcode-scanner/

For complete explanation you can find my other answer here: https://stackoverflow.com/a/54474247/6617276