# Storage isn't keeping data

**URL:** <https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527>\
**Category:** ionic-v3\
**Created:** [November 17, 2016, 11:20pm UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527 "2016-11-17T23:20:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bssanchez](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bssanchez](https://forum.ionicframework.com/u/bssanchez)\
**Post date:** [November 17, 2016, 11:20pm UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527/1 "2016-11-17T23:20:25Z")

</div>

Hi there,

i have a problem with Storage, i have this provider:

```
import { Injectable } from '@angular/core';
import { JwtHelper, tokenNotExpired } from 'angular2-jwt';
import { Storage } from '@ionic/storage';
import 'rxjs/add/operator/map';

@Injectable()
export class Storejwt {
    private jwtHelper: JwtHelper;

    constructor(private local: Storage) {
        this.jwtHelper = new JwtHelper();
    }

    /**
     * Obtener Dato por Clave desde el Storage
     */
    public getDato(clave: string): any {
        return this.local.get(clave);
    }

    /**
     * Asignar dato a una Clave en el Storage
     */
    public setDato(clave: string, valor: any): any {
        return this.local.set(clave, valor);
    }

    /**
     * Remover Dato por Clave del Storage
     */
    public delDato(clave: string): any {
        return this.local.remove(clave);
    }

    /**
     * Decodificar token en formato JWT
     */
    public decodeToken(token: any): any {
        let rtnData: any = null;

        try {
            rtnData = this.jwtHelper.decodeToken(token);
        } catch (e) {
            console.error('Storejwt Error (decodeToken): ' + e)
        }

        return rtnData;
    }

    /**
     * Verificar si el token a expirado o no (True: aún no ha expirado, False: ya expiró)
     */
    public isActive(token: any) {
        let tk_expired = false;
        try {
            tk_expired = tokenNotExpired(null, token);
        } catch (e) {
            console.error('Storejwt Error (isActive): ' + e)
        }
        return tk_expired;
    }

}

```

yes, it is in app.module.ts (into providers), and when i login i do this:

```
this._storeJwt.setDato('id_token', data);

```

yes, it’s imported and injected in constructor. But when i change page, id\_token data is lost for example, i call this function:

```
let token = this._storeJwt.getDato('id_token');

```

and it’s null 😕

What i have done bad?, thanks in advance.

PD: I did npm install @ionic/storage --save, and cordova-sqlite-storage (yes, the cordova plugins is innecesary for my case, but…)

---

<div class="post-metadata">

**Author:** ![infoproducts](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/infoproducts/32/100372_2.png) [@infoproducts](https://forum.ionicframework.com/u/infoproducts)\
**Post date:** [November 18, 2016, 1:20am UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527/2 "2016-11-18T01:20:51Z")

</div>

I assume you’re testing on device, since you’re mentioning cordova-sqlite-storage.

I ran into a similar issue. Turns out somewhere I was attempting to write to storage before platform was ready. Make sure to wrap calls to ionic-storage in platform ready calls.

```auto
this.platform.ready().then(() => {
  this.local.set('key', 'val');
});

```

---

<div class="post-metadata">

**Author:** ![esolution](https://avatars.discourse-cdn.com/v4/letter/e/bcef8e/32.png) [@esolution](https://forum.ionicframework.com/u/esolution)\
**Post date:** [November 18, 2016, 3:00am UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527/3 "2016-11-18T03:00:13Z")

</div>

Storage return a promise, you must also use promise on your providers

---

<div class="post-metadata">

**Author:** ![infoproducts](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/infoproducts/32/100372_2.png) [@infoproducts](https://forum.ionicframework.com/u/infoproducts)\
**Post date:** [November 18, 2016, 3:01am UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527/4 "2016-11-18T03:01:31Z")

</div>

^ This.

Didn’t notice how @bssanchez was retrieving the token.

---

<div class="post-metadata">

**Author:** ![bssanchez](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bssanchez](https://forum.ionicframework.com/u/bssanchez)\
**Post date:** [November 21, 2016, 5:26pm UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527/5 "2016-11-21T17:26:50Z")

</div>

Yes, thanks so much…

i change my get function to:

```
/**
 * Obtener Dato por Clave desde el Storage
 */
public getDato(clave: string): any {
    return new Promise((valor) => {
        this.local.get(clave).then((_valor) => {
            valor(_valor);
        });
    });
}

```

and it’s works 😃

---

<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:** [November 21, 2016, 9:13pm UTC](https://forum.ionicframework.com/t/storage-isnt-keeping-data/70527/6 "2016-11-21T21:13:57Z")

</div>

It looks to me like you have fallen into [the explicit promise constructor antipattern](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern) pit.
