角度5:无法读取未定义的属性“订阅”

时间:2019-05-25 11:05:25

标签: angular async-await rxjs observable subscribe

我已经尝试了很多天来解决stackoverflow上的错误,因为我现在对angular还是陌生的,所以我只需要向社区询问。我一直在进行JWT身份验证

  

错误TypeError:无法读取未定义的属性“ subscribe”       在RawMaterialComponent.loadRawMaterialsByCompany(raw-material.component.ts:774)

我在组件中有如下服务呼叫

    private loadRawMaterialsByCompany(callback: any) {
    let serviceSubscriptions =this.rawMaterialService.getRawMaterialsByCompany().subscribe(data => {
      this.rawMaterials = data;
      this.rawMaterialLoading = false;
    }, 
     error => this.handleError(error));
    }

并且服务如下

     getRawMaterialsByCompany() {
        const url = this.raw_material_api_endpoint + 'Company/' + this.company_id;
        return this.get(url);
      }

上面的服务调用重定向到基本服务,并且它实现了所有http请求,即:get,post

我要在这里执行的操作是,如果访问令牌已过期,我想刷新令牌。使用发帖请求

     protected get(url: string) {
        if (this.isTokenExpired(localStorage.getItem('userToken'))) {

            this.getOriginalDataset(url).then((val)=>{

            this.getTokenfromService()
            return(val)
          });
        } else {
          return this.http.get(url)
            .map(this.extractRequests)
            .catch(this.handleError);
        }


    protected post(url: string, body: any) {
        if (url.includes("RefreshToken") || url.includes("Login") || url.includes("Logout")) {
          return this.http.post(url, body, { headers: this.getHeaders() })
            .map(this.extractRequests)
            .catch(this.handleError);
        }
        else {
            return this.http.post(url, body, { headers: this.getHeaders() })
              .map(this.extractRequests)
              .catch(this.handleError);

        }

      }

**现在,我不会为发布请求刷新令牌

getOriginalDataset()方法为原材料组件带来请求的数据

getTokenfromService()方法获取新的访问令牌并刷新令牌以存储在本地存储中

    sendoriginalDataset(originalUrl):Promise<any>{
        return new Promise(resolve => {
        const dataSet= this.http.get(originalUrl, { headers: this.getHeaders() })
        .map(this.extractRequests)
        .catch(this.handleError);
        resolve(dataSet)
        });
      }

    async getTokenfromService() {

        const base64EncodedJwtAndRefreshToken = btoa(encodeURI(`${localStorage.getItem('userToken')}:${localStorage.getItem('refreshToken')}`));
        const url = this.login_api_endpoints + 'RefreshToken/';
        this.asyncResult = await this.post(url, JSON.stringify(base64EncodedJwtAndRefreshToken))
          .toPromise();
        console.log('First Promise resolved.')
        if (this.asyncResult !== null) {
          localStorage.setItem('userToken', this.asyncResult.jwtToken)
          localStorage.setItem('refreshToken', this.asyncResult.refreshToken)

        }

      }

这两种方法都可以正常运行,但是一旦getTokenfromService()执行了发布请求,控制台错误会弹出 无法读取未定义的属性'subscribe'

每个http请求都会发生这种情况,上面是一个示例。小小的帮助真的很感激!

2 个答案:

答案 0 :(得分:2)

您无法subscribe承诺。更改:

this.getOriginalDataset(url).then((val)=>{
this.getTokenfromService()
return(val)

收件人:

return from(this.getOriginalDataset(url)).pipe(
  tap(() => this.getTokenfromService())
)

答案 1 :(得分:1)

您必须使用等待来获取数据

await this.getTokenfromService()

如果您仍然有任何错误,请告诉我

相关问题