Storage return undefined value when calling from another page


#1

hi all
i have strange issue
i using (( Storage )) as on https://ionicframework.com/docs/storage/
but when i set storage from page and return value from another page
on first time that i calling this value i get undefine value but other times it give the correct value

why ??
can some one help me please


#2

Please show us the code you’re using on two components and how they interact with each other. Hard to tell what’s going on without seeing any code.


#3

hi luukschoen,
on first page i put this on button (click)
setValue(){
this.storage.set(‘key’, ‘hhhhh’)
}

second page

import { Component, OnInit } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import {Storage} from ‘@ionic/storage’;

@Component({
selector: ‘page-about’,
templateUrl: ‘about.html’
})
export class AboutPage implements OnInit {
private val1: string;
private val2: string;

constructor(public navCtrl: NavController,
private storage: Storage) {}

ngOnInit() {
this.storage.get(‘key’).then((value) => {
console.log('Inside '+value);
this.val1 = value;
});

console.log('Outside ' + this.val1);

}

getTestParam(){
console.log(this.val1);
/*
this.storage.get(‘key’).then((value) => {
console.log(value);
this.val2 = value;
});
*/
}

}


#4

thanks , i solve the problem

the problem that i must put every thing inside (( then ))

this.storage.get(‘key’).then((value) => {

    (( put every thing here )) 

});
thanks again