How to take {Storage} value in await manner in constructor?

Friends,
I need to take some values in constructor from {Storage} in await mode . In my case the processing did not wait for async function to complete … My constructor is

constructor(public navCtrl: NavController,
   public navParams: NavParams,
   public formbuilder: FormBuilder,
   public http: HttpClient,
   public propertyProvider: PropertyProvider,
   private storage: Storage) {
   this.fetchLBSettings();  
   alert("lbid is:"+this.lbid);  // GOT UNDEFINED HERE
   //this.lbid = 290; //test case amboori panchayath
   //this.distid = 1; // test case TVM
   //this.lbtype = 5; //test case for panchayath
   //this.lbname = "Amboori Grama Panchayat";
   this.data.result = '';

   this.getZonalOffice(this.lbid,this.lbtype);
   this.getAssessmentYear(this.lbid);
   this.formgroup = formbuilder.group({
     zonaloffice:['',Validators.required],
     wardyear:['',Validators.required],
     wardnumber: ['',Validators.required],
     doornumber: ['',Validators.required],
     subnumber: ['']
   });
   this.zonaloffice = this.formgroup.controls['zonaloffice'];
   this.wardyear = this.formgroup.controls['wardyear'];
   this.wardnumber = this.formgroup.controls['wardnumber'];
   this.doornumber = this.formgroup.controls['doornumber'];
   this.subnumber = this.formgroup.controls['subnumber'];
   
   this.data.zonaloffice = '';
   this.data.wardyear = '0';
   this.data.wardnumber = '';
   this.data.doornumber = '';
   this.data.subnumber = '';
 }

my async function is

async fetchLBSettings()
 {
   let lbid = await this.storage.get('lbid');
   let lbname = await this.storage.get('lbname');
   let distid = await this.storage.get('distid');
   let lbtype = await this.storage.get('lbtype');
   if(lbid) {
     alert("set");
   } else {
     alert("not set mone");
     this.storage.set('lbid', 171);
     this.storage.set('lbname', 'Kozhikodu Corporation');
     this.storage.set('distid', 11);
     this.storage.set('lbtype', 4);
   }
 }

I need to take the else part value on three variables…

Please any body help asap

Thanks

Anes

@anespa I think u had better use the ionic lifecycle hooks to solve this issue.Usually its takes time to get data from storage.
https://blog.ionicframework.com/navigating-lifecycle-events/

Hi Vibin ,

I fixed that problem by removing all items from constructor. We could not write async stuffs in constructor. So I put them in ionViewDidEnter () and make it async…

Thanks

1 Like