“在使用离子存储的clear()和remove()函数时无法读取未定义的属性'set'

问题描述 投票:1回答:1

我正在尝试为我的项目创建一个简单的登录和注销系统,在该系统中,一旦我登录,详细信息将使用“ @ ionic / storage”库存储在本地存储中,然后在初始化和使用时检索这些详细信息当应用程序运行时。

一切正常,但是当我单击“联系人图标”(在我的情况下为注销按钮=>参见下面的屏幕截图)时,错误弹出

“错误TypeError:无法读取未定义的属性'set'”

。我是ionic 2和angular 2开发的新手,但我无法解决这个问题!我希望有关该问题的信息足够,并期待解决方案。预先谢谢!

home.ts文件如下所示。

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

import { FirebaseService } from '../../services/firebase.service';
import { Storage } from '@ionic/storage';
import { Lot } from '../../classes/lot'
import { Handler } from '../../classes/handler';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  //variable to track the app state
  appState: string = "default";
  loginError: boolean = false;

  login: boolean = false;
  logout: boolean = false;

  //active variables
  activeLotKey: string;
  activeLot: Lot;

  //lots variable that contains a list of parking lots in that area
  handlers: Handler[];
  handler: Handler;

  //constructor function => initializes the lots variable
  constructor(public navCtrl: NavController, public platform: Platform, public _fs: FirebaseService, public storage: Storage) {
    this._fs.getHandlers().subscribe(handlers => this.handlers = handlers);
    platform.ready().then(() => {
      storage.ready().then(() => {
        storage.get('handler').then((handler) => {
          if(handler){ 
            this.handler = handler;
            console.log(this.handler);
            this._fs.getLot(this.handler.lotKey).subscribe(lot => {
              this.activeLot = lot[0];
              console.log(this.activeLot); 
            });
            //console.log(this.lot);
          } else {
            this.appState = 'login';
            console.log('chaning state to ' + this.appState);
          }
        });
      });
    });
  }

  logIn(username: string, password: string){
    for(let handler of this.handlers){
      if(handler.username == username && handler.password == password){
        console.log("Username : " + username + " and Password " + password + " is correct...");
        this.handler = handler;
        this.handler.isLoggedIn = true;
        this._fs.updateHandler(this.handler.$key, this.handler);
        this.loginError = false;
        this.setHandler();
        this.getHandler();
        return;
      }
    }
    console.log("Username : " + username + " and Password " + password + " is incorrect!!!");
    this.loginError = true;
  }

  logOut(){
    this.handler.isLoggedIn = false;
    this._fs.updateHandler(this.handler.$key, this.handler);
    this.removeHandler();
    this.getHandler();
  }

  setHandler(){
    this.storage.ready().then(() => {
      this.storage.set("handler", this.handler).then((d) => {
        console.log("the handler is written to local storage", d);
      }, (e) => {
        console.log("Error while inserting ", e);
      });
    });
  }

  getHandler(){
    this.storage.ready().then(() => {
      this.storage.get('handler').then((handler) => {
        if(handler){
          this.handler = handler;
          console.log('get storage function' + handler);
          this._fs.getLot(handler.lotKey).subscribe(lot => {
            this.activeLot = lot[0];
            console.log(this.activeLot); 
          });
          this.appState = 'default';
          console.log('Changing appState to default');
        } else {
          this.appState = 'login';
          console.log('chaning state to ' + this.appState);
        }
      });
    });
  }

  removeHandler(){
    this.storage.ready().then(() => {
      this.storage.clear().then((d) => {
        console.log("the handler is removed from the local storage", d);
      }, (e) => {
        console.log("Error while removing ", e);
      });
    });
  }

  changeState(state: string, lot?: Lot){
    if(lot){
      this.activeLot = lot;
      this.activeLotKey = lot.$key;
    }
    console.log('changing state from ' + this.appState + ' to ' + state);
    this.appState = state;
  }
}
angular ionic-framework ionic2 local-storage
1个回答
0
投票

您的问题是clear()

清除整个键值存储。警告:热!

[使用clear()时,从handlerget都没有。因此,您需要使用remove(key)

请参阅doc here about clear()

© www.soinside.com 2019 - 2024. All rights reserved.