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.
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.
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.
If @joshmorony’s suggestion isn’t appropriate (if, for example, the two values are set independently), Promise.all
would be another option.
Try this out:
storage.forEach((index, key, value) => {
console.log(index, key, value);
});
This will return you everything stored in your storage.
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.
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’]);
})