Reputation: 565
I have data stored using custom keys. I want to return all data that starts with the id KEY-
so I loop over all the data and storage and push the ones that match into an array. I can see that the data is being pushed into data[]
but when I call getData()
in my component it returns an empty array. What am I doing wrong?
storage.ts
getData(): any {
this.storage.ready().then(() =>
{
let data = [];
this.storage.forEach((value, key, index) =>
{
if (value.id.startsWith("KEY-")) {
data.push(value);
console.log(data);
}
});
return data;
});
}
component.ts
ionViewDidLoad() {
this.data = this.storage.geData();
}
I've also tried this way
ionViewDidLoad() {
this.storage.getData().then(data => console.log(data));
}
Upvotes: 1
Views: 105
Reputation: 5796
So just return a promise from your function then try to resolve data
with this promise.
getData(): any {
return this.storage.ready().then(() =>
{
let data = [];
this.storage.forEach((value, key, index) =>
{
if (value.id.startsWith("KEY-")) {
data.push(value);
console.log(data);
}
});
return data;
});
}
Upvotes: 2