Ionic Native Printer module : TypeError: Cannot read property 'printer' of undefined

Hello,
I am facing an error (in Ionic v3) when I try to implement the printer module like explained in the docs :

Here is my code :

  • app.module.ts :
...
import { Printer, PrintOptions } from '@ionic-native/printer';

@NgModule({
  declarations: [
    MyApp
    //HomePage
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp),
    HomePageModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Printer
  ]
})
export class AppModule {}
  • add-user.ts (my page) :
import { Printer, PrintOptions } from '@ionic-native/printer';

  constructor(public navCtrl    : NavController,
              public http       : HttpClient,
              public NP         : NavParams,
              public fb         : FormBuilder,
              public toastCtrl  : ToastController,
              public alertCtrl  : AlertController,
              private printer   : Printer)
  {
...   
  }

  printQR() : void{
      this.printer.isAvailable().then(function(){
        this.printer.print("http://www.google.com").then(function(){
          alert("printing done successfully !");
        },function(){
          alert("Error while printing !");
        });
    }, function(){
      alert('Error : printing is unavailable on your device ');
    });
  }

The error that I got is this one (below), however I don’t see anything int the docs that says I have to define the printer property :

vendor.js:1823 ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘printer’ of undefined
TypeError: Cannot read property ‘printer’ of undefined
at :8080/build/1.js:268
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:5134)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:5125)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)
at c (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:5125)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)
at o (polyfills.js:3)
defaultErrorLogger @ vendor.js:1823

Could you help me with this issue ?

I’ve just found what was the problem, so I put my code here in order that other people that face the same issue could deal with it :
The problem was that I defined each time new functions, and so I lost each time the context that “this” was always based upon, and so the “this” was always undefined. Instead of that I should have used arrow functions, which does not create a new context, like this :

  printQR() : void{
      this.printer.isAvailable().then(onSuccess =>{
        this.printer.print("https://www.google.com").then(onSuccess =>{
          alert("printing done successfully !");
        },onError=>{
          alert("Error while printing !");
        });
    }, onError =>{
      alert('Error : printing is unavailable on your device ');
    });
  }