ionic3ionic-storage

Ionic 3 Storage Issue Not Working in Browser


Ok I'm about to rip my hair out (what's left of it). Whenever I start up the app from scratch after clearing all cookies/storage items I check for certain items...thus seeing if the person is logged in or not into the app.

After having them log in via firebase (which is successful) it returns back to the app where we then set the UID and obtain the google token for our API calls. All of the values are confirmed and are being retrieved successfully.

We set these via a storage service provider. Once these are set, we attempt to call something from our API, this needs the google token. When we attempt to retrieve that from storage it is not there. However, if I just refresh my browser (not clearing cookies/etc) in the debugging/application tab, magically the localdb values are there and now everything works.

Here is the storage provider:

@Injectable()
export class StorageProvider {
constructor(public http: HttpClient, public storage: Storage) {
console.log('Hello StorageProvider Provider');
}
public set(settingName,value):Promise<any>{

return this.storage.ready().then(() => {
  this.storage.set(`setting:${ settingName }`,value).then(() => {
    Promise.resolve(true);
  })
  .catch(err => {
    Promise.reject(err);
  })
})
.catch(err => {
  console.log("ERROR Storage Not Ready: " + err);

})
} public async get(settingName){
return this.storage.ready().then(() => { 
  this.storage.get(`setting:${ settingName }`).then((res) => {
      Promise.resolve(res);
  })
  .catch(err => {
    Promise.reject(err);
  })
})
.catch( err => {
  console.log("Storage Get Error");
  return Promise.reject(err);
}
);
}

Can someone see what I am doing wrong? Here's a quick snippet of how we call it which always returns the No Token Set before the CMD + R. Once I do the CMD + R everything is there and it all works flawlessly.

await this.storage.get('Token').then((results) => {
  this.token = results;
})

if(this.token == null)
{
  return Promise.reject("No Token Set");
}

Solution

  • Try:

    this.token = await this.storage.get(‘Token’);
    

    Instead of how you use await in async method.

    Also good read: https://www.joshmorony.com/using-asyncawait-syntax-for-promises-in-ionic/