# How to get and return stored data - StorageService?

**URL:** <https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167>\
**Category:** ionic-v3\
**Created:** [September 29, 2018, 5:55am UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167 "2018-09-29T05:55:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HippoRoo](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@HippoRoo](https://forum.ionicframework.com/u/HippoRoo)\
**Post date:** [September 29, 2018, 5:55am UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/1 "2018-09-29T05:55:40Z")

</div>

Hello,

I’m new to Ionic. I want to get and return the stored data, but it looks like I need to use Promise to make this code work and I do not know how to use Promise. I appreciate anyone who can look at my code below and give me advise on how to make this code work properly:

Provider: storage-service.ts:  
I’m able to store and output data, but I cannot return the data:

```
import {Injectable} from '@angular/core';
import { Storage } from '@ionic/storage';

@Injectable()
export class StorageService {

constructor(public storage: Storage){}

public storeData (data) {
     this.storage.set('data', data);
  }

public getStoredData() {
    	this.storage.get('data').then((val) => {
    	console.dir(val); // ****** this outputs the object.
            return val; // ***** this returns undefined
     });
}

```

}

* * *

my-page.ts:

```
import {StorageService} from "../../providers/storage-service";

constructor(public storageService: StorageService) {

        // ***** this outputs undefined, what I'm doing wrong?
        console.dir(this.storageService.getStoredData());
}
```

---

<div class="post-metadata">

**Author:** ![hirenkorat3](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hirenkorat3/32/128872_2.png) [@hirenkorat3](https://forum.ionicframework.com/u/hirenkorat3)\
**Post date:** [September 29, 2018, 5:58am UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/2 "2018-09-29T05:58:26Z")

</div>

try this

```auto
public getStoredData() {
    return this.storage.get('data');
}

```

---

<div class="post-metadata">

**Author:** ![HippoRoo](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@HippoRoo](https://forum.ionicframework.com/u/HippoRoo)\
**Post date:** [September 29, 2018, 6:17am UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/3 "2018-09-29T06:17:19Z")

</div>

Thank you - this worked; however, the data that is returned is an object. I tried this code and it does not work:  
let user\_hash=this.storageService.getStoredData()[‘user\_hash’];  
I also tried this and it does not work:  
let user\_hash=this.storageService.getStoredData().user\_hash;  
Could you please help on how do I get the value of the object name: user\_hash?

---

<div class="post-metadata">

**Author:** ![hirenkorat3](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hirenkorat3/32/128872_2.png) [@hirenkorat3](https://forum.ionicframework.com/u/hirenkorat3)\
**Post date:** [September 29, 2018, 6:42am UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/4 "2018-09-29T06:42:27Z")

</div>

refer this link i hope you will get all your answer here

> **[How to Store Data inside Ionic Apps - Ionic Academy](https://ionicacademy.com/store-data-inside-ionic/)**
>
> Welcome to the 5. lesson of the Ionic Crash Course! Today we will tackle another very common obstacle when building a mobile app: Where to...

---

<div class="post-metadata">

**Author:** ![etonyali](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@etonyali](https://forum.ionicframework.com/u/etonyali)\
**Post date:** [May 13, 2019, 6:37pm UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/5 "2019-05-13T18:37:54Z")

</div>

You need to make the getStoredData() function promise and then when calling it, use .then to get your data out.

Get Data Function

```auto
getStorage(key: string): Promise<any> {
    return this.storage.get(key);
  }

```

Retrieving it

```auto
this.storage.getStorage('uid').then(uid => {
      // you can do anything here. I used uid as example because I use firebase and this saves me calls.
});

```

---

<div class="post-metadata">

**Author:** ![danieldugger](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@danieldugger](https://forum.ionicframework.com/u/danieldugger)\
**Post date:** [May 14, 2019, 12:56am UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/6 "2019-05-14T00:56:11Z")

</div>

Don’t forget that not key and value are strings. You’ll need to use JSON.parse

---

<div class="post-metadata">

**Author:** ![etonyali](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@etonyali](https://forum.ionicframework.com/u/etonyali)\
**Post date:** [May 14, 2019, 2:11pm UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/7 "2019-05-14T14:11:02Z")

</div>

They should be DOMString which returns a string type. You wouldn’t want to put object, array, and/or number as your key.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:15pm UTC](https://forum.ionicframework.com/t/how-to-get-and-return-stored-data-storageservice/143167/8 "2021-07-19T21:15:50Z")

</div>

Closed on July 19, 2021, 9:15pm UTC


