Why My Condition always return to false condition?


#1

Hi i tried to maintain login session , so i uses storage native to save the credential to check if users is login or not . And then i add condition when data that stored in native storage not null then the rootpage will be set in homepage, and when the data stored is null the rootpage will be set to loginpage. But i don’t know why my condition will always return else either the data null or not. Eventough i though my condition already right but it always return else. here is my code

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import {LoginPage} from '../pages/login/login'
import { HomePage } from '../pages/home/home';
import {NativeStorage} from '@ionic-native/native-storage'
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any ;
  storedauth: string;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private nativestorage : NativeStorage) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      this.nativestorage.getItem('Auth').then(
        data => this.storedauth = data.token
      )
      if(this.storedauth != null){
        this.rootPage = HomePage;
      }
      else{
        this.rootPage = LoginPage;
      }
      splashScreen.hide();
    });
  }
}

Can you guys tell me why is this not working ? and it always return false


#2

I Just notice that this.storedauth is always null, it will be only have data when the page is active . Can you help me here guys ?


#3

Use an observable: https://angular.io/guide/observables

Also see:


#4

CLOSED , there is an error in my code. thanks anyway bro

 this.nativestorage.getItem('Auth').then
      ( data=>  { 
        if(data != null){
         // this.navCtrl.setRoot(HomePage);
         this.rootPage = HomePage;

        }       
      })

that is my fix