Ionic 2 storage - get all stored keys forEach method

Hi there!
Can please someone give me an example on how I can get all available stored keys in Ionic storage using forEach method of storage object?

thanks!

3 Likes

Never used ionic-storage, but looking at the implementation here

/**
* Iterate through each key,value pair.
* @param iteratorCallback a callback of the form (value, key, iterationNumber)
*/
forEach(iteratorCallback: (value: any, key: string, iterationNumber: Number) => any) {
return this._db.iterate(iteratorCallback);
}

I guess this might work:

this.storage.forEach( (value, key, index) => {
	console.log("This is the value", value)
	console.log("from the key", key)
	console.log("Index is" index)
})
11 Likes

It works! Thanks very much. Do you know if there are any limitations regarding the size that can be stored using Ionic storage? The docs are saying that “Storage will prioritize using SQLite”, so I wonder if the same limits as SQLite are applied for the Ionic storage too.

best!
d.

1 Like

I’m glad I could help :slight_smile:
About your latest question, I’m really not sure.

Thanks, works for me too!

Yes, this works and it can iterate items from storage!
But how can I fetch items on html using *ngFor?

I see items displaying in console.log but I have no idea on how to put them in an array which I can use with *ngFor let of directive…

in your .ts file:

extractData () {

this.storage.get ('name of your key'). then ((val) => {

      this.Content = val;

      this.Reports = [this.Content];

          });
    }

in your .html file (use *ngFor )

<ion-list>
     <ion-item *ngFor="let report of Reports;" tappable>
         <ion-avatar item-left>
           <img src="img/camarista.png"> 
        </ion-avatar>
        <ion-label text-wrap>
            <h1>
                <span col-10 class="informe">{{report.title}}  <!--
You get the value of an element contained in the array -->
                  <ion-icon name="arrow-forward"></ion-icon></span>
              
            </h1>
            

        </ion-label>
      </ion-item>
    </ion-list>

(the additional elements depend on your need)