Reference to THIS is null

Hi,

Im using BarcodeScanner plugin and when I try to access “this” variable inside a then() callback, a reference to this variable is null.
I also tried to use a NgZone but had the same result.

My code:

this.scanner
      .scan()
      .then(function (barcodeData) {
        
        
        console.log(barcodeData.text); //OK - print the barcode
        console.log(this); //print null
        console.log(this.currentlyScanning); //gives a error


      }, function (error) {
        alert(error);
      });

ionic info

  @ionic/cli-utils  : 1.19.1
    ionic (Ionic CLI) : 3.19.1

global packages:

    cordova (Cordova CLI) : 7.1.0

local packages:

    @ionic/app-scripts : 1.3.7
    Cordova Platforms  : ios 4.5.4
    Ionic Framework    : ionic-angular 3.3.0

System:

    ios-deploy : 1.9.2
    ios-sim    : 5.0.13
    Node       : v8.9.4
    npm        : 5.6.0
    OS         : macOS High Sierra
    Xcode      : Xcode 9.2 Build version 9C40b

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : legacy

Can someone help me?
Thanks!

If you use function() { }, it won’t pass the context to the function.

Use this syntax, as described in the docs.

this.barcodeScanner.scan().then((barcodeData) => {
 // Success! Barcode data is here
}, (err) => {
    // An error occurred
});

Thank you @MattE ! It solved my problem.