I am working on Angular4. I have something that I don't undestand. I store a value on the service provider and then I get it from a component. This is my code:
gard 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));
}
);
}
When I try 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
}
the value of key is undefined. Why is that?