Usertype based login


#1

hi guys i am new in ionic 2,
facing problem related to login based on userRoll
i have created login html and function. and it worked perfectly before if/else condintion added
after adding if/else condition showing error:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘UserRole’ of null
TypeError: Cannot read property ‘UserRole’ of null

i want to push my page to ‘StudentCornerPage’ if (UserRole==3)
or to ‘TStudentAttendencePage’ if (UserRole==2)
my login API returns { UserRole, name, present, etc etc}
please help me
here is my code:

constructor(public navCtrl: NavController,
public navParams: NavParams,
public storage: Storage,
public userProvider: UserProvider,
public menuCntlr: MenuController,) {

this.storage.get('token').then((value) => {
//  **showing error after adding this**
  if (value.UserRole==3) {
    console.log('Token : ' + value);
    this.navCtrl.setRoot("StudentCornerPage");
  }
  if( value.UserRole==2){
    this.navCtrl.setRoot('TStudentAttendencePage');
  }
});

this.loginDTO = { UserName: "", Password: "" };
//this.menuCntlr.swipeEnable(false);

}

createAccount() {
this.navCtrl.push(‘RegisterPage’);
}

login() {

console.log("login clickd");
this.userProvider.login(this.loginDTO).then(data => {
  if (data) {
    this.storage.set('token', JSON.stringify(data));
    this.user=JSON.stringify(data);
    console.log('user:'+this.user);
     //  **showing error after adding this**
    if(this.user.UserRole==2){
      console.log('userRole:'+this.user.userRole);
      this.navCtrl.push('TStudentAttendencePage');
    }
    if(this.user.UserRole==3){
      this.navCtrl.push('StudentCornerPage');
    }
    
  }
});

}


#2

look at this example …
I think it will help you:
import {Storage} from ‘./…/…/providers/storage’;

        export class Login {
             user = {
          emailOrMobile: "",
               password: ""
              }
  constructor(
    public storage: Storage,
        )
           login() {
          this.storage.userLogin(this.user)
            .subscribe((res) => {      
                if (res.UserRole==3) {
                    
                   this.navCtrl.setRoot("StudentCornerPage");
                 
                }
              if (res.UserRole==2) {
                  this.navCtrl.setRoot('TStudentAttendencePage');
                
                 
                }

                }
            );
        }

this function on storage page.
userLogin(userData){
let data = JSON.stringify(Object.assign(userData));
return this.http.post(this.api.API_URL+‘users.php?action=loginUser’, data).map(res=>res.json());
}