Hello,
I’m new to Ionic. I want to get and return the stored data, but it looks like I need to use Promise to make this code work and I do not know how to use Promise. I appreciate anyone who can look at my code below and give me advise on how to make this code work properly:
Provider: storage-service.ts:
I’m able to store and output data, but I cannot return the data:
import {Injectable} from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class StorageService {
constructor(public storage: Storage){}
public storeData (data) {
this.storage.set('data', data);
}
public getStoredData() {
this.storage.get('data').then((val) => {
console.dir(val); //****** this outputs the object.
return val; //***** this returns undefined
});
}
}
my-page.ts:
import {StorageService} from "../../providers/storage-service";
constructor(public storageService: StorageService) {
//***** this outputs undefined, what I'm doing wrong?
console.dir(this.storageService.getStoredData());
}