sam fisher
sam fisher

Reputation: 13

problem with load nativeStorage at first run

I need help to solve this problem. In home.ts I check if user had logged in before with nativeStorage

home.ts

ionViewWillEnter() {
  this.load();
}

load() {

  this.chk = this.gettoken();

  alert(this.chk)
  if (this.chk == 'true')

  {
    //// code here
  }

  gettoken(): Promise<string> {
    this.nativeStorage.getItem('isLoggedIn').then((value) => {
      this.val = value;
    });
    return this.val;
  };

}

and in login page I save to storage using this :

this.nativeStorage.setItem('isLoggedIn','true');

At first time run app I got 'undefined' and it's ok now. After I login successfully and close the app and run it again I got 'undefined' and I have to go to another page and back again to home page to get the value of storage (isLoggedIn) true

I change gettoken() to this:

async gettoken(){
    return await this.storage.getItem('isLoggedIn');
}   

but the same problem

Upvotes: 0

Views: 35

Answers (1)

KeeyPee
KeeyPee

Reputation: 310

then you need to wait for .gettoken() too, by doing:

async load(){
    this.chk = await this.gettoken();
    alert(this.chk);
}

Upvotes: 1

Related Questions