# SQL storage Ionic 2 How to use the get method

**URL:** <https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748>\
**Category:** ionic-v3\
**Created:** [May 11, 2016, 11:07am UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748 "2016-05-11T11:07:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benschuro](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@Benschuro](https://forum.ionicframework.com/u/Benschuro)\
**Post date:** [May 11, 2016, 11:07am UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/1 "2016-05-11T11:07:42Z")

</div>

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 :

```auto
       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

---

<div class="post-metadata">

**Author:** ![itsmemyk](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/itsmemyk/32/100236_2.png) [@itsmemyk](https://forum.ionicframework.com/u/itsmemyk)\
**Post date:** [May 11, 2016, 11:47am UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/2 "2016-05-11T11:47:47Z")

</div>

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);
  })
}
```

---

<div class="post-metadata">

**Author:** ![Benschuro](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@Benschuro](https://forum.ionicframework.com/u/Benschuro)\
**Post date:** [May 11, 2016, 12:57pm UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/3 "2016-05-11T12:57:24Z")

</div>

Thank you itsmemyk. It helped me !

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [May 11, 2016, 7:02pm UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/4 "2016-05-11T19:02:12Z")

</div>

> [@itsmemyk](#):
>
> Solution 1

IMHO Solution 1 is an example of [the explicit Promise construction antipattern](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns).

---

<div class="post-metadata">

**Author:** ![CarlosLiro](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@CarlosLiro](https://forum.ionicframework.com/u/CarlosLiro)\
**Post date:** [September 7, 2016, 10:05am UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/5 "2016-09-07T10:05:25Z")

</div>

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);
        })
    });
  }
```

---

<div class="post-metadata">

**Author:** ![itsmemyk](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/itsmemyk/32/100236_2.png) [@itsmemyk](https://forum.ionicframework.com/u/itsmemyk)\
**Post date:** [September 13, 2016, 7:35am UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/6 "2016-09-13T07:35:48Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![CarlosLiro](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@CarlosLiro](https://forum.ionicframework.com/u/CarlosLiro)\
**Post date:** [September 21, 2016, 6:18am UTC](https://forum.ionicframework.com/t/sql-storage-ionic-2-how-to-use-the-get-method/51748/7 "2016-09-21T06:18:57Z")

</div>

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