harptom
harptom

Reputation: 49

Angular & Ionic 4 - Await and async not working as intended

I am trying to display error messages to the user when they are signing up. I can't figure out why I keep getting the following errors on my code.

error TS1308: 'await' expression is only allowed within an async function.

Here is the code:

async signUp() {
     firebase.auth().createUserWithEmailAndPassword(
     this.realemail, this.realpassword

     ).then((user) => {
      this.navCtrl.navigateForward("username-sign-up");
    }).catch((err) => {
      console.log(err);
      const errmessage = await this.toastCtrl.create({
        message: err.message,
        duration: 3000
      });
      await errmessage.present();
    })
   }

Upvotes: 2

Views: 278

Answers (1)

Ferenc
Ferenc

Reputation: 537

Try adding async to your catch handler. It's a method like any other, so in order to call async in, it needs to be async itself.

async signUp() {
     firebase.auth().createUserWithEmailAndPassword(
     this.realemail, this.realpassword

     ).then((user) => {
      this.navCtrl.navigateForward("username-sign-up");
    }).catch(async (err) => {
      console.log(err);
      const errmessage = await this.toastCtrl.create({
        message: err.message,
        duration: 3000
      });
      await errmessage.present();
    })
   }

Upvotes: 3

Related Questions