Retrieving multiple key values โโfrom Ionic2 storage
Using Ionic2 repository, I did the following in a custom provider, I can confirm that the key-value pairs are set and I can get them individually:
this.storage.set('one', 1);
this.storage.set('two', 2);
this.storage.set('three', 3);
The problem is that I have a function in one of my pages that needs to access all three of these values โโfor an HTTP request, so I can get one of them like this:
this.storage.get('one').then((value) => {
// Preform http request sending value in POST data
});
But how do I get multiple key values โโat once? Do I need to embed these calls or is there an easier way to access multiple keys in one call so that I can do something like:
this.storage.get(['one', 'two', 'three']).then((values) => {
console.log(values.one); // I dunno, something like this
});
+3
source to share
2 answers
You can simply do it like below.
this.storage.forEach( (value, key, index) => {
console.log("value", value);//store these values as you wish
console.log("key", key);
console.log("Index" index);
})
/**
* Iterate through each key,value pair.
* @param iteratorCallback a callback of the form (value, key, iterationNumber)
* @return Promise that resolves when the iteration has finished.
*/
forEach(iteratorCallback: (value: any, key: string, iterationNumber: Number) => any): Promise<null> {
return this._dbPromise.then(db => db.iterate(iteratorCallback));
}
+2
source to share