TypeError: this is undefined


#1

Hello, i have two functions :
This one checks if a username is already used in database:

public async checkIfExistUsername(){
      this.authServiceProvider.getDataz("user/"+this.userData.username)
    .then(data => {
      if(data!=null) this.usernameTaken=1;
    }, (err) => {
      console.log("Couldn't check email");
    });

  }

This one checks if email is already used in database:

public async checkIfExistEmail(){
    this.authServiceProvider.getDataz("userbyemail/" + this.userData.email)
    .then(data => {
      if(data!=null) this.emailTaken=1;
    }, (err) => {
      console.log("Couldn't check email");
    });

  }

and i have this function that does some popups and shit if email/username exist

 whatDo(){
    
      if(this.usernameTaken!=0 && this.emailTaken!=0){
        let alert = this.alertCtrl.create({
          title: 'Erreur',
          subTitle: 'Nom d\'Utilisateur & Email déjà utilisé par un autre Compte.',
          buttons: ['ok']
        });
        alert.present();
      }else if(this.usernameTaken!=0){
        let alert = this.alertCtrl.create({
          title: 'Erreur',
          subTitle: 'Nom d\'Utilisateur déjà utilisé par un autre Compte.',
          buttons: ['ok']
        });
        alert.present();
      }else if(this.emailTaken!=0){
        let alert = this.alertCtrl.create({
          title: 'Erreur',
          subTitle: 'Cet Email déjà utilisé par un autre Compte.',
          buttons: ['ok']
        });
        alert.present();
      }else{
        
        this.showEtape1 = false;
        this.showEtape2 = true;
        this.showEtape3 = false;
        this.showEtape4 = false;
      }
  }

And here’s my getDataz function in AuthService provider:

 public getDataz(path): Promise<any> {
    return new Promise((resolve, reject) => {
      this.http.get(apiUrl + path).subscribe((response) => {
        resolve(response);
      }, (err) => {
        reject(err);
      })
    })
      .catch((err) => {
        throw err;
      });
  }

Im calling these tree functions like this:

 this.checkIfExistEmail()
     .then(this.checkIfExistUsername)
     .then(this.whatDo);

Here’s the error log:

Error: Uncaught (in promise): TypeError: this is undefined
[59]/SignupPage.prototype.checkIfExistUsername/</<@http://localhost:8100/build/main.js:1388:17
step@http://localhost:8100/build/main.js:1249:18
verb/<@http://localhost:8100/build/main.js:1230:53
[59]/__awaiter</<@http://localhost:8100/build/main.js:1224:15
t@http://localhost:8100/build/polyfills.js:3:21506
[59]/__awaiter<@http://localhost:8100/build/main.js:1220:12
[59]/SignupPage.prototype.checkIfExistUsername@http://localhost:8100/build/main.js:1385:16
F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14974
onInvoke@http://localhost:8100/build/vendor.js:4982:24
F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14901
F</c</r.prototype.run@http://localhost:8100/build/polyfills.js:3:10124
f/<@http://localhost:8100/build/polyfills.js:3:20240
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
onInvokeTask@http://localhost:8100/build/vendor.js:4973:24
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562
F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
o@http://localhost:8100/build/polyfills.js:3:7887
F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16823
p@http://localhost:8100/build/polyfills.js:2:27646
v@http://localhost:8100/build/polyfills.js:2:27893

Stack trace:
c@http://localhost:8100/build/polyfills.js:3:19752
c@http://localhost:8100/build/polyfills.js:3:19461
f/<@http://localhost:8100/build/polyfills.js:3:20233
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
onInvokeTask@http://localhost:8100/build/vendor.js:4973:24
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562
F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
o@http://localhost:8100/build/polyfills.js:3:7887
F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16823
p@http://localhost:8100/build/polyfills.js:2:27646
v@http://localhost:8100/build/polyfills.js:2:27893

#2

I used

this.checkIfExistEmail()
     .then(this.checkIfExistUsername.bind(this))
     .then(this.whatDo.bind(this));

Credits to Suraj Rao from Stackoverflow