首页 文章

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

提问于
浏览
1

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

一切正常但是当我点击“联系人图标”(在我的情况下退出按钮=>参考下面附带的屏幕截图)时会弹出错误

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

. 我是离子2和角度2开发的新手,我无法解决这个问题!我希望有关问题的信息足够,并期待一个解决方案 . 先感谢您!

注意:此项目的Github链接! github.com/pmithunish/my-handler-app在测试用户名时使用这些详细信息:“pmithunish”和密码:“mit940”

屏幕截图如下 .

enter image description here

enter image description here

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;
  }
}

1 回答

  • 0

    这里你的问题是 clear() .

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

    当你使用 clear() 时,没有 handlerget . 所以你需要使用 remove(key) 代替 .

    请参阅doc here about clear() .

相关问题