Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4: loading.present is not a function

Ionic 4, getting an error while using the loader loading.present is not a function

Here is my code:

const loading =  this.loadingController.create({
  message: 'Loading',
});

loading.present();
like image 411
Varun Sukheja Avatar asked Jun 13 '26 03:06

Varun Sukheja


1 Answers

This is because loadingController.create() is an asynchronous method and before you get the instance of HTMLIonLoadingElement in variable loading, you are calling the loading/present() which is undefined for the moment.

So you need to wait until you get the instance of HTMLIonLoadingElement while calling loadingController.create()

How to solve it: Simple use aync/await

const loading = await this.loadingController.create({
  message: 'Loading',
});
loading.present();

See we used await just after the = operator. So it makes sure next line to be executed only when call to loadingController.create is complete and variable loading is initialized.

NOTE: Don't forget to add async keyword to the function inside which you are using the loader code, as we are using await.

like image 66
Varun Sukheja Avatar answered Jun 18 '26 01:06

Varun Sukheja