Get multiple values at same time using Ionic Storage

Hello,

I used Ionic Storage.
I saved this data:

this.storage.set('key1', 'value1');
this.storage.set('key2', 'value2');

How I can get all data at same time, example: for compare:

if (key1 == key2) {
  // do something
}

Greetings.

1 Like

The best way would probably be the store both values as a single object, i.e:

let data = {
  key1: 'value1',
  key2: 'value2'
};

then just load that object in.

2 Likes

If @joshmorony’s suggestion isn’t appropriate (if, for example, the two values are set independently), Promise.all would be another option.

1 Like

Try this out:

storage.forEach((index, key, value) => {
	console.log(index, key, value);
});

This will return you everything stored in your storage.

1 Like

I’m not seeing how that returns anything.

Take a look here: https://github.com/driftyco/ionic-storage/blob/master/src/storage.ts

   **
   * Iterate through each key,value pair.
   * @param {any} iteratorCallback a callback of the form (value, key, iterationNumber)
   * @returns {Promise} Returns a promise that resolves when the iteration has finished.
   */
  forEach(iteratorCallback: (value: any, key: string, iterationNumber: Number) => any): Promise<void> {
    return this._dbPromise.then(db => db.iterate(iteratorCallback));
  }

So

storage.forEach((index, key, value) => {
	console.log(index, key, value);
});

should return every stored key<->value pair with the index.

Again, I don’t see it returning anything. I see it logging things to console, which is useless when one is trying to do something with the data.

1 Like

Thank You

data = {
key1: ‘value1’,
key2: ‘value2’
};

data2: string = ‘’;

this.storage.set(this.data2, this.data);
this.storage.get(this.data2).then((val) => {
console.log(‘data is’, val[‘key1’]);
})