微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

####### Listing services #######
[node1] (local) root@192.168.0.8 ~
$ 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) root@192.168.0.8 ~
$ 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) root@192.168.0.8 ~

####### 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调用需要时间才能完成,换句话说它是异步调用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?