然后如何在Ionic 4上的方法中显示presentAlert?

时间:2019-06-17 09:26:33

标签: firebase alert ionic4

我试图在“ then方法”中显示一个presentAlert,在集合上添加一个文档(Firebase + Ionic 4,如果成功,它应该显示警报)。但是问题是,如果我从then方法内部进行调用,我将无法到达presentAlert方法。

  addObject() {
this.afs.collection("Objects").add(this.object)
  .then(function () {
    console.log("Object successfully written!");
    this.presentAlert() //Doesn't work
  })
  .catch(function (error) {
    console.error(error);
  }); 
 }

  async presentAlert() {
    const alert = await this.alertController.create({
      header: 'Thank you!',
      message: 'This Object has been uploaded succesfully :)',
      buttons: ['OK']
    });
    await alert.present();
  }

1 个答案:

答案 0 :(得分:0)

更改以下内容:

  .then(function () {
    console.log("Object successfully written!");
    this.presentAlert() //Doesn't work
  })
  .catch(function (error) {
    console.error(error);
  }); 
 }

对此:

.then(() => {
    console.log("Object successfully written!");
    this.presentAlert() //Doesn't work
  })
  .catch((error) => {
    console.error(error);
  }); 
 }