I am attempting to assign values from a JSON response to some variables. However, when I try to retrieve and display these values using Console.log(), they are returning as "undefined."
Can anyone help me identify what mistake I might be making in this scenario?
public data: any;
carbs: string;
fat:any;
protein:any;
constructor(public navCtrl: NavController,public userprovider: UserProvider) {
//fetches JSON array
this.user = this.userprovider.getUser(userid);
this.user.toPromise().then(res => {
this.carbs = res[0].carbs;
this.fat = res[0].fat;
this.protein = res[0].protein;
});
console.log(this.carbs);
//data assignment section
this.data = {"Macros":[{"Macros":"Prot","time":this.protein,"color":"#3fa9f5","hover":"#57c0f5"},{"Macros":"Carb","time":this.carbs,"color":"rgb(236, 240, 241)","hover":"rgb(236, 240, 241)"},{"Macros":"Fat","time":this.fat,"color":"rgb(52, 73, 94)","hover":"rgb(52, 73, 94)"}]};
}