变量从 http 调用的响应回调返回中获取未定义的值

问题描述

我的服务中有这个函数可以很好地返回响应:

####### Listing services #######
[node1] (local) [email protected] ~
$ docker service ls
ID             NAME      MODE         REPLICAS   IMAGE          PORTS
syn9jo2t4jcn   app2      replicated   1/1        Nginx:latest   

####### Scalling app2 from single container to 10 more container #######
[node1] (local) [email protected] ~
$ docker service update --replicas 10 app2
app2
overall progress: 10 out of 10 tasks 
1/10: running   [==================================================>] 
2/10: running   [==================================================>] 
3/10: running   [==================================================>] 
4/10: running   [==================================================>] 
5/10: running   [==================================================>] 
6/10: running   [==================================================>] 
7/10: running   [==================================================>] 
8/10: running   [==================================================>] 
9/10: running   [==================================================>] 
10/10: running   [==================================================>] 
verify: Service converged 
[node1] (local) [email protected] ~

####### Verifying that app2's workload is distributed to both of the ndoes #######
$ docker service ps app2
ID             NAME      IMAGE          NODE      DESIRED STATE   CURRENT STATE            ERROR     PORTS
z12bzz5sop6i   app2.1    Nginx:latest   node1     Running         Running 15 minutes ago             
8a78pqxg38cb   app2.2    Nginx:latest   node2     Running         Running 15 seconds ago             
rcc0l0x09li0   app2.3    Nginx:latest   node2     Running         Running 15 seconds ago             
os19nddrn05m   app2.4    Nginx:latest   node1     Running         Running 22 seconds ago             
d30cyg5vznhz   app2.5    Nginx:latest   node1     Running         Running 22 seconds ago             
o7sb1v63pny6   app2.6    Nginx:latest   node2     Running         Running 15 seconds ago             
iblxdrleaxry   app2.7    Nginx:latest   node1     Running         Running 22 seconds ago             
7kg6esguyt4h   app2.8    Nginx:latest   node2     Running         Running 15 seconds ago             
k2fbxhh4wwym   app2.9    Nginx:latest   node1     Running         Running 22 seconds ago             
2dncdz2fypgz   app2.10   Nginx:latest   node2     Running         Running 15 seconds ago  

在 http 拦截器中,我想获取返回的 response.token :

public refreshToken(): string {
    const token_refresh = localStorage.getItem('token_refresh');
    this.http.post<any>(environment.baseApiUrl + 'token_refresh',{token_refresh},this.requestOptions).subscribe(
      response => {
        localStorage.setItem('token',response.token);
        return response.token;
      }
    );
  }

但我得到的不是令牌值,而是 token = this.authService.refreshToken();

我想这可能与异步行为有关,但我没有找到如何获得正确的返回值。

解决方法

这不是带有 observable 的 http 的工作方式。

由于 oberservable 的异步特性,拦截器不会等到 http 请求完成。

相反,您应该将 observable 返回给拦截器并在那里订阅。

,

请写这个。

 token = this.authService.refreshToken().subscribe(tokenComingFromService=>{
 console.log(tokenComingFromService);
 })

因为 Service 总是返回一个 observable,你需要订阅才能得到它。

,

你可以做到这一点,让承诺不订阅。

 public refreshToken(): string {
    const token_refresh = localStorage.getItem('token_refresh');
    return this.http.post<any>(environment.baseApiUrl + 'token_refresh',{token_refresh},this.requestOptions).toPromise();
  
  }

另一边

 this.authService.refreshToken.then(response =>{
    token = response.token
    localStorage.setItem('token',response.token);
  });

或 使用 observable 试试这个

public refreshToken(): string {
  const token_refresh = localStorage.getItem('token_refresh');
  return this.http.post<any>(environment.baseApiUrl + 'token_refresh',this.requestOptions)
}

在拦截器中

token = this.authService.refreshToken().subscribe(
  response => {
    localStorage.setItem('token',response.token);
     response.token;
  }
);

这是因为http调用需要时间才能完成,换句话说它是异步调用