Search code examples
cordovagoogle-chromeionic3ionic-storage

Ionic Testing in Chrome with IonicStorage


I'm building an Ionic 3 App using the new IonicStorage. I'm setting a bearerToken as so...

//store the BearerToken
setToken(token){
this.storage.set('bearertoken',token);
}

All works well and everything comes back very nicely...until....I make a change to the code and live reload happens. It should retrieve the token as the platform is ready as such:

getToken(){
this.storage.get('bearertoken')
.then((val) => {
  return val;
})
}

But the value is always empty as if it totally got wiped. Is this the way it should be when testing in the browser?


Solution

  • I think you need to do it via a var. Not sure what you are trying to achieve by returning a value in in the callback.

    // declare a var before constructor in your component:
    token: string = "";
    
    getToken(){
      this.storage.get('bearertoken')
        .then((val) => {
          this.token = val;
        })
    }