简体   繁体   中英

Ionic / Angular 5 - Storage get in external function

I am trying to get a value with ionic local storage. But it always returns undefined . This is my code snippet.

    getValue(k) {
        this.storage.get(k).then((val) => {
            console.log(val);
            return val;
        });
    }

let test = this.storageprovider.getValue('test');

The test is always undefined .

Can anyone help me in fixing this?

See code inside the getValue fn is returning the promise. So you need to use

this.storageprovider.getValue('test').then((result)=>{
   console.log(result);
});

async getLocalData(){
   let test = await this.storageprovider.getValue('test');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM