SQL storage Ionic 2 How to use the get method

Hi !
I want to use SQL Storage in my Ionic 2 app to store the user token after he is logged in.
When I use the get method I receive an object of type :
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array[0]}
with my token in the zone_symbol value array but I cannot retrieve the token from the array.

I use the get method like this :

       return this.local.get('token').then(
         (token)=> {
         console.log(token); 
         return token;
         })
         .catch(error => 
         console.error(error)
         );

I don’t understand how to handle the promise that is returned by the get method of local storage.

My ionic info are :
Ionic Framework Version: 2.0.0-beta.6
Ionic CLI Version: 2.0.0-beta.25
Ionic App Lib Version: 2.0.0-beta.15
OS:
Node Version: v5.11.0

Thank you by advance

Hi @Benschuro,

Your problem is not about local storage, the problem is of returning token inside then handler.
you cannot return value directly from promise, either you can return promise object or new promise if you want to do any reusable manipulation task.

Solution 1

getToken() {
 return new Promise((resolve, reject) => {
   this.local.get("token").then((tokenValue) => {
     resolve(tokenValue);
   }).catch( error => {
     reject(error);
   })
 });
}

constructor() {
  this.getToken().then(val => {
    console.log(val);
  })
}

Solution 2 (Simple)

getToken() {
  return  this.local.get("token");
}

constructor() {
  this.getToken().then(val => {
    console.log(val);
  })
}
1 Like

Thank you itsmemyk. It helped me !

IMHO Solution 1 is an example of the explicit Promise construction antipattern.

1 Like

Hi @itsmemyk

I followed your solution and it seems that the value is returned properly but I don’t know why it keeps in a never ending loop between the two methods. This is my code. Hope someone can help. Thanks in advance

public authenticated() {
  var isAuthenticated = this.isAuthenticated().then(
    alfTicket => { 
      console.log(alfTicket); 
      return alfTicket; 
    }
  );
  return isAuthenticated !== null; }

Then as per your solution

private isAuthenticated() {
    return new Promise((resolve, reject) => {
      this.local.get("alf_ticket").then((alfTicket) => {
          resolve(alfTicket);
        }).catch( error => {
          reject(error);
        })
    });
  }

Hi @CarlosLiro,

There is silly mistake of how you are consuming isAuthenticated() promise method. in authenticated() method, you putting statement “isAuthenticated !== null;”, which will compare promise object with !== null (not with promise value, what you are returning). So authenticated must be some promise. Its matter of how you are using authenticated method, you use following approach.

public authenticated() {
   return this.isAuthenticated()
         .then(alfTicket => alfTicket !== null);
}

 private isAuthenticated() {
    return new Promise((resolve, reject) => {
       this.local.get("alf_ticket")
      .then((alfTicket) => {
            resolve(alfTicket);
       }).catch( error => {
            reject(error);
       });
    });
 }

you should utilized authenticated() method as follow.

this.authenticated().then((flag) => console.log('isAuthenticated', flag);
1 Like

Hi @itsmemyk. Silly me! now I understand what was going wrong and know how to use promises bit better…