I am currently engaged in Angular4 development and have encountered an issue that I cannot seem to resolve. The problem arises when I attempt to store a value on the service provider and retrieve it from a component. Below is a snippet of my code:
Service Provider:
key:any;
constructor(){}
storeKeysAppPreferences(res){
this.appPreferences.clearAll();
console.log("storeKeysAppPreferences",res);
this.appPreferences.store('key1',JSON.stringify(res));
}
fetchKeysAppPreferences(){
this.appPreferences.fetch("key1").then(
(res) => {
this.key=(JSON.parse(res));
}
);
}
Upon calling console.log()
fetchKeysAppPreferences(){
this.appPreferences.fetch("key1").then(
(res) => {
this.key.push(JSON.parse(res));
console.log(this.key); //is definded
}
);
console.log(this.key); // undefined
}
Surprisingly, the value of key turns out to be undefined. Can anyone shed some light on why this might be happening?