Cannot get "this" from callback

Hey guys, im grinding my teeth here lol

i load a json with

readJSON(filename) {

this.file.readAsText(this.file.externalRootDirectory + "xtracker", filename).then(() => this.successCallback, this.failureCallback);

}

but in

successCallback(result) { console.log(this)};

ig get undefined , means i cannot fill the variable for my ngfor list, this.clients

i tried some hacks like ()=> but to no avail

why do you use like this? Why just perfom like:

readJSON(filename) {

this.file.readAsText(this.file.externalRootDirectory + "xtracker", filename).then(() => {
  console.log('success');
}).catch(error => {
  console.error('error', error);
});
}

This is about function scope and being able to make sure this this is the right this.

  readJSON(filename) {
    this.file
      .readAsText(this.file.externalRootDirectory + 'xtracker', filename)
      .then(this.successCallback, this.failureCallback);
  }

That should do the trick.

thanx guys but that didnt do the tick … in the callback this is not the same this , so i cannot set my array for my ngfor loop with this.myArray …

i used an ugly hack , decalring This = this; then i can use This instead

This is why I recommend never passing non-static methods as callbacks. Did you try @EinfachHans’s suggestion? Inside his arrow function, this should be what you expect.