I am facing a weird issue, I am getting undefined in a global variable which I want to use it later. I have put 2 console.log(), the tricky part is Check 2 (as console.log output) fires first instead of Check 1 ). Please have a look at the TS file. In the Check 1, I get the value correctly but it fires after the setRadioButton method, I am little confused as to why it happens like that?
import { Component } from '@angular/core';
;import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ToastController } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage';
@Component({
selector: 'page-profile',
templateUrl: 'profile.html'
})
export class ProfilePage {
userImg : any;
userName : string;
profileType :string;
instr : boolean;
stud :boolean;
constructor(public navCtrl: NavController, private nativeStorage : NativeStorage) {
this.nativeStorage.getItem("userLogin").then((data)=>{
this.userImg = data.picture;
this.userName = data.name;
}).catch((c)=>console.log(c));
this.nativeStorage.getItem("userProfile").then((data)=>{
this.profileType = data.userProfileType;
console.log("Check 1",this.profileType);
}).catch((c)=>console.log(c));
this.setRadioButton()
}
setRadioButton()
{
console.log("Check 2",this.profileType);
if(this.profileType == "Instructor")
{
this.instr = true;
console.log("I")
}
if(this.profileType == "Student"){
this.stud = true;
console.log("S")
}
}
}
And the console log output
FCMPlugin.js: is created
FCMPlugin.js:41 FCMPlugin Ready OK
bootstrap.js:10 Ionic Native: deviceready event fired after 1822 ms
index.js:411 DEVICE READY FIRED AFTER 1738 ms
profile.ts:29 Check 2 undefined
profile.ts:22 Check 1 Instructor