Cannot read property 'set' of undefined(…) Secure Storage Ionic

Actually I’m working with ionic 2 and Secure Storage, What I did, first of all

ionic plugin add cordova-plugin-secure-storage

After I installed the plugin,

import { Component }        from '@angular/core';
import { NavController }    from 'ionic-angular';
import { Platform}          from 'ionic-angular';
import { AlertController }  from 'ionic-angular';

import { Facebook}          from 'ionic-native';
import { SecureStorage }    from 'ionic-native';

import { TourPage }         from '../tour/tour';
import { HomePage }         from '../home/home';
import { LoginService }     from './login.service';


@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  providers: [LoginService]
})
export class LoginPage {
  tourPage = TourPage;
  secureStorage:SecureStorage = new SecureStorage();

  constructor(public nav: NavController,
              public alert: AlertController,
              public loginService:LoginService
             ) {

    this.secureStorage.create('mibank').then(
      () => console.log('Storage is ready!'),
      error => console.log(error)
    );
  }

  loginFb(){
    ...

    var self = this;
    self.loginService.register(token).subscribe(
        (user)=>{ self.saveUser(user)},
        error => console.error(`Error: ${error}`));
  }    

  saveUser(user:any) {
    console.log('save user...',user);
    this.secureStorage.set('token',user.token).then(
        data => {
            return this.secureStorage.get('tourShown');
        },
        error => console.log(error)
    ).then( (tourShown)=> {
        console.log('is tour shown?: ',tourShown);
        if(tourShown){
          this.nav.setRoot(HomePage);
        }else{
          this.nav.setRoot(TourPage);
        }
    });
  }
}

I always get the error

Cannot read property 'set' of undefined(…)

What is wrong in the above code?

2 Likes