Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 Loadingcontroller overlay doesnt exist

I created a simple function of creating a loading like this

async presentLoading() {
  const loading = await this.loadingController.create({
    message: 'Please Wait...',
  });
  await loading.present();
}

And i am closing the loader when the data is fetch like this

  getUserData(){
  console.log(this.userID);
    this.api.getCompanyEmploye(this.userID).subscribe(res => {
     this.loadingController.dismiss(); //closing here 

      console.log(res);
      this.user = res.records[0];
      this.familyMembers = res.records[0].family_members;
    });
  }

I am calling both function in constructor

constructor(public loadingController: LoadingController){
  this.presentLoading();
  this.getUserData();
}

Its showing error of ERROR Error: Uncaught (in promise): overlay does not exist

like image 482
Umaiz Khan Avatar asked Dec 23 '19 12:12

Umaiz Khan


1 Answers

The issue is that your API call responds sooner than the loading controller gets instantiated. Instead of parallel calls, you should try to serialize those this way:

Make your presentLoading method to return Promise:

async presentLoading() {
  const loading = await this.loadingController.create({
    message: 'Please Wait...',
  });
  return loading.present();
}

Now you can call it this way:

getUserData(){
  this.presentLoading().then(()=>{
     this.api.getCompanyEmploye(this.userID).subscribe(res => {
        this.loadingController.dismiss(); //closing here 
        console.log(res);
        this.user = res.records[0];
        this.familyMembers = res.records[0].family_members;
    });
  })
}

And in your constructor you need only to call for the API

like image 170
Sergey Rudenko Avatar answered Oct 10 '22 03:10

Sergey Rudenko