Get multiple values at same time using Ionic Storage


#1

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.


#2

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.


#3

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


#4

Try this out:

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

This will return you everything stored in your storage.


#5

I’m not seeing how that returns anything.


#6

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.


#7

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.