简体   繁体   English

ionic 2存储无法使用变量

[英]ionic 2 storage not working with variables

I am not sure what the problem is here but i can't return a value. 我不确定这是什么问题,但我无法返回值。 I do not want to use sqlite right now. 我现在不想使用sqlite。

 doLogin(event){
    this.storage.get('user').then((val) => {
      this.username=val;
      console.log(this.username);
      console.log(val);
  });

}

The problem is the 1st log returns undefined but the second log is returning the correct values. 问题是第一个日志返回未定义,但是第二个日志返回正确的值。

Edit : Really sorry guys. 编辑:真的很抱歉 Both the logs are working fine. 两个日志都工作正常。 But the log mentioned below is not working. 但是下面提到的日志不起作用。 This is causing the if statement to return false even though all the values match Full code for the file 即使所有值都与文件的完整代码匹配,这也会导致if语句返回false

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FormBuilder, Validators } from '@angular/forms';
import { Storage } from '@ionic/storage';

@Component({
  selector: 'login',
  templateUrl: 'login.html'
})
export class LoginPage {

   username: string;
   password: string;

  public loginForm = this.fb.group({
    password: ["", Validators.required],
    user: ["", Validators.required],

  });

  constructor(public navCtrl: NavController, public fb:FormBuilder, public storage:Storage) {

  }


  doLogin(event){
    this.storage.get('user').then((val) => {
      this.username=val;
      console.log( this.username);
      console.log(val);
  });

  this.storage.get('password').then((val) => {
       this.password=val;
  });
  console.log(this.username); //NOT WORKING
      if( this.username == this.loginForm.get('user').value &&  this.password == this.loginForm.get('password').value)
          {
            console.log("Login Successful");
          }


  }

Promises are asynchronous..that console log statement is likely called before the db returns val . 承诺是异步的。在数据库返回val之前,可能会调用控制台日志语句。 You can do this operation within then 您可以在then进行此操作

If you are using promises, you can use Promise.all 如果您使用诺言,则可以使用Promise.all

let userCredentials = [this.storage.get('user'),this.storage.get('password')];
Promise.all(userCredentials).then(values =>{
  this.username = values[0];
  this.password = values[1];
   if( this.username == this.loginForm.get('user').value &&  this.password == this.loginForm.get('password').value)
      {
        console.log("Login Successful");
      }
});

Promise.all will call the array of promises and wait till all return. Promise.all将调用Promise数组并等待所有返回。

We can use Observable from RxJs to check 2 promises from storage finish. 我们可以使用RxJs中的Observable检查存储完成后的2个Promise。

Observable.forkJoin([
   Observable.fromPromise(this.storage.get('user')), 
   Observable.fromPromise(this.storage.get('password'))
])
.subscribe(data=> {
    this.username = data[0];
    this.password = data[1];
    if (this.username == this.loginForm.get('user').value &&  this.password ==this.loginForm.get('password').value){
        console.log("Login Successful");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM