【发布时间】:2018-11-23 00:51:36
【问题描述】:
我在调用订阅中的函数时遇到问题。在这个函数中,我调用了一个刷新令牌的订阅,如果返回“true”,我将调用相同的函数,但这个操作不起作用。
主要功能:
getLicenseList():Observable<License[]>{
let licenseList:License[] =[];
return this._http.get("license/list").map(res => {
let processedData = res;
if(processedData['status'] == "401"){
this._http.refreshToken().subscribe(result => {
this.getLicenseList();
});
}else{
for(let license of processedData['data']){
const obj = new License(license['name'],license['start_date'],license['expire_date'], license['type'], license['duration'],license['id'],license['id_user'], license['projectsList']);
licenseList.push(obj);
}
return licenseList;
}
});
}
刷新令牌的功能:
refreshToken():Observable<boolean>{
let url = 'auth/refresh/self';
return this.post(url, localStorage.getItem('refresh_token')).map(data => {
if(data['access_token'] != null || data['refresh_token'] != null){
window.localStorage.setItem('access_token', data['access_token']);
window.localStorage.setItem('refresh_token', data['refresh_token']);
return true;
}
}).catch(error =>Observable.throw("Expired refresh token"));
}
在调试模式下进入 res 可以看到:“Unexpected end of input”
【问题讨论】:
-
getLicenseList将始终返回licenseList,因为另一个返回语句取决于异步操作
标签: angular rest model-view-controller subscription