【问题标题】:Storage in Ionic not saving value into variables properlyIonic 中的存储不能正确地将值保存到变量中
【发布时间】:2018-02-25 20:03:03
【问题描述】:

我想将 this.data 作为 post 请求的参数发送,但是当我在 return 语句之前放置一个 console.log(this.data) 时,它返回的 token 和 regNo 都是空值,但在 then 方法中storage get,console.log(this.data) 给出正确的值。这里出了什么问题?

import { Injectable } from "@angular/core";
import { Http } from '@angular/http';
import { Storage } from '@ionic/storage';

import 'rxjs/add/operator/toPromise';
import { DiaryModel } from './diary.model';

@Injectable()
export class DiaryService {
  constructor(public http: Http, public storage: Storage) {}

    data: any = {token: null, regNo: null};

    getData(): Promise<DiaryModel> {
      this.storage.get('regNo').then((val) => {
            console.log(val);
            this.data.regNo = val;

          this.storage.get('user').then((val2) => {
              console.log(val2);
              this.data.token = val2.token;
          });
      });
      return this.http.post("http://www.mysite.xyz/services/service.php", this.data)
               .toPromise()
               .then(response => response.json() as DiaryModel)
               .catch(this.handleError);
    }

  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}

【问题讨论】:

    标签: javascript typescript ionic-framework ionic2


    【解决方案1】:

    Ionic 用作非阻塞 I/O 模型。在您的代码中,返回语句将在从存储方法获取值之前运行。您必须使存储方法和 return 语句同步,因此 return 语句等待存储方法解析值。

    getData(): Promise<DiaryModel> {
        return new Promise((resolve) =>{
            data: any = {token: null, regNo: null};
    
            this.storage.get('regNo').then((val) => {
                console.log(val);
                this.data.regNo = val;
    
                this.storage.get('user').then((val2) => {
                    console.log(val2);
                    this.data.token = val2.token;
                });
            });
    
            resolve(data);
        });
    }
    
    returnData(): Promise<any> {
        this.Data().then(res => {
            return this.http.post("http://www.mysite.xyz/services/service.php", this.data)
                   .toPromise()
                   .then(response => response.json() as DiaryModel)
                   .catch(this.handleError);
        }
    }
    

    然后可以调用returnData()方法获取return语句。

    【讨论】:

    • 还是不行。仍然遇到同样的问题。其他问题似乎是.. :(
    • 您忘记返回存储访问承诺。
    • @SurajRao 我该怎么做?
    • @ShafeefOmar 你试过我的回答了吗?
    • @ShafeefOmar 如果您想要这个答案,请添加return this.storage.get('regNo').then((val) =&gt; {return this.storage.get('user').then((val2) =&gt; {
    【解决方案2】:

    您需要链接承诺以按顺序获取数据。因为它们是异步的。 您的 http 请求正在发送之前存储返回值。

    我愿意:

     getData(): Promise<DiaryModel> {
          let regPromise = this.storage.get('regNo');
          let tokenPromise =  this.storage.get('user');
          return Promise.all([regPromise,tokenPromise]).then(values=>{
             this.data.regNo=values[0];
             this.data.token = values[1].token;
             return this.http.post("http://www.mysite.xyz/services/service.php", this.data)
                   .toPromise()
                   .then(response => response.json() as DiaryModel)
                   .catch(this.handleError);
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2018-03-04
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多