First time local storage value getting null


#1

First time login when i try get local storage value on that time local storage value getting null but send time login and try to get local storage value its working file working fine

I’m set the value into the local storage like this

           this.storage.set('loggedIn', this.loggedIn);
           this.storage.set('username',this.user_name);
           this.storage.set('userid', this.userid);
           this.storage.set('license', this.license);
           this.storage.set('userType', this.userType);

And get like this

ionViewDidLoad() {
          this.storage.get('license').then(lic => {
          alert(this.license =lic)
          });
         this.storage.get('username').then(user => {
          alert(this.gaurdname =user);
         
          });
             this.storage.get('userid').then(id => {
             alert(this.user_id =id)
         
          });   
  }

Please any one help me out
Thanks


#2

I am also facing this issue since last night, before its working properly.
Give some suggestion if any one have resolved this issue.

Thanks


#3

You can try like this

constructor(){

	   let self = this;
   setInterval(function () {
    self.getData();
  }, 300);	
}

 getData(){
 this.storage.get('username').then(val => {
          this.username =val;
          }); 
}

#4

If u need setTimeout to resolve async nature of storage then u are missing a fundamental point

U need to chain the storage-set promises (or use Promise.all) and after completion set a flag to say all is stored. And use that in thr storage get sonewhere else to see if there is data stored before u get it.