Angular 6-如何以正确的方式从另一个异步函数的异步函数的回调中获取价值?

时间:2019-01-21 14:05:46

标签: typescript promise angular6

我有以下HTTP提供程序,使用下面的示例中的subscription方法调用它。问题是在GET请求期间,我需要获取保存在本地存储中的令牌值,这是使用HTTP提供程序中的getToken函数中的另一个提供程序发生的。我无法在HTTP调用中获取令牌值。我想问一下,如何以正确的方式做到这一点?

谢谢您的任何建议。

this.httpProvider.doGetRequest(Constants.API_SERVER_URL+Constants.API_ENDPOINT_THREADS)
      .subscribe(res => {
        console.log(res);
        this.loading.dismiss();
      }, (error) => {
        console.log(error);
        this.err = error;
        this.loading.dismiss();
        this.presentToast();
      });

HTTP提供程序类:

@Injectable()
export class HttpProvider {

  constructor(public http: HttpClient,
              public dataProvider: DataProvider,
              public locStor: LocalStorageProvider) {
    console.log('Hello HttpProvider Provider');
  }

  public doGetRequest(url: string) {
    return this.http.get(url, this.getHeaders());
  }

  public doPostRequest(url: string, params: any) {
    return this.http.post(url, params);
  }

  public getHeaders() {
    return {
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'Bearer test',
        'token': this.getToken()
      },
      params: new HttpParams()
    };
  }

  private getToken() {

    this.locStor.getValue(Constants.LS_KEY_USER_TOKEN).then((val) => {
      return val;
    });
    //return this.locStor.getValue(Constants.LS_KEY_USER_TOKEN);
    //return '222';
    //return this.dataProvider.getUser().token;
  }

}

更新版本

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {HttpHeaders} from "@angular/common/http";
import {LocalStorageProvider} from "../local-storage/local-storage";
import {Constants} from "../../app/constants";
import {Headers} from "@angular/http";
import {HttpParams} from "@angular/common/http";
import {DataProvider} from "../data/data";
import 'rxjs/add/observable/fromPromise';
import { Observable } from 'rxjs/Observable';
import {FromObservable} from "rxjs-compat/observable/FromObservable";
import {PromiseObservable} from "rxjs-compat/observable/PromiseObservable";


/*
  Generated class for the HttpProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class HttpProvider {

  constructor(public http: HttpClient,
              public dataProvider: DataProvider,
              public locStor: LocalStorageProvider) {
    console.log('Hello HttpProvider Provider');
  }

  public doGetRequest(url: string) {
    //return this.http.get(url);

    return Observable.fromPromise((this.getHeaders()).flatMap((headers) => {
      this.http.get(url, headers);
    }));

  }

  public doPostRequest(url: string, params: any) {
    return this.http.post(url, params);
  }

  public getHeaders() {
    /*
    return {
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'Bearer test',
        'token': this.getToken
      },
      params: new HttpParams()
    };
    */

    return this.locStor.getValue(Constants.LS_KEY_USER_TOKEN).then((val) => {
      return {
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Authorization': 'Bearer test',
          'token': val
        },
        params: new HttpParams()
      };
    });

  }

  private getToken() {
    this.locStor.getValue(Constants.LS_KEY_USER_TOKEN);
    /*
    this.locStor.getValue(Constants.LS_KEY_USER_TOKEN).then((val) => {
      return val;
    });
    */
    //return this.locStor.getValue(Constants.LS_KEY_USER_TOKEN);
    return '222';
    //return this.dataProvider.getUser().token;
  }

}

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

CREATE TABLE parent (
    id INTEGER NOT NULL, 
    PRIMARY KEY (id)
);

CREATE TABLE child (
    id INTEGER NOT NULL, 
    parent_id INTEGER, 
    PRIMARY KEY (id), 
    FOREIGN KEY(parent_id) REFERENCES parent (id)
);


-- FOREIGN KEY constraint failed: INSERT INTO child (parent_id) VALUES (42) 
INSERT INTO child (parent_id) VALUES (42)