【问题标题】:Angular & Ionic 4 - Await and async not working as intendedAngular 和 Ionic 4 - 等待和异步未按预期工作
【发布时间】:2020-05-02 08:18:18
【问题描述】:

我试图在用户注册时向他们显示错误消息。我不知道为什么我的代码总是出现以下错误。

错误 TS1308:“await”表达式只允许在异步函数中使用。

代码如下:

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();
    })
   }

【问题讨论】:

    标签: javascript angular firebase ionic-framework firebase-authentication


    【解决方案1】:

    尝试将异步添加到您的 catch 处理程序。这是一个和其他方法一样的方法,所以为了调用异步,它本身需要是异步的。

    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();
        })
       }
    

    【讨论】:

    • 我想你也可以在 signUp() 之前删除异步注释
    猜你喜欢
    • 2019-04-19
    • 2020-03-04
    • 2019-04-07
    • 2022-01-22
    • 2018-12-02
    • 1970-01-01
    • 2021-07-15
    • 2021-12-08
    • 2023-03-03
    相关资源
    最近更新 更多