Javascript http调用在promise回调函数2中未触发
在angular 2项目中,我打电话获取一些数据:Javascript http调用在promise回调函数2中未触发,javascript,angular,promise,observable,angular2-http,Javascript,Angular,Promise,Observable,Angular2 Http,在angular 2项目中,我打电话获取一些数据: this.http.getfromgraphql() .map((response: Response) => response.json()) .subscribe( (data) => { console.log(data); }); 这将触发下面显示的getfromgraphql函数,但在调用之前,我必须在checkaccesstokenvalidi
this.http.getfromgraphql()
.map((response: Response) => response.json())
.subscribe(
(data) => {
console.log(data);
});
这将触发下面显示的getfromgraphql函数,但在调用之前,我必须在checkaccesstokenvalidity中检查访问令牌是否仍然有效,在那里我设置了一些变量并解析该函数。控制台登录“然后”,但不会触发我的http post。
为什么会这样?
另外,我相信有更好的方法可以做到这一点,但就我的一生而言,我无法找到更好的解决方案,我非常感激
checkAccessTokenValidity() {
let headers = new Headers();
headers.append( 'Content-Type', 'application/x-www-form-urlencoded' );
let options = new RequestOptions({ headers: headers });
// if(!this.access_token) {
return new Promise((resolve, reject) => {
this.http.post(this.URL + 'oauth/token', this.authentication, options)
.subscribe(function(response) {
this.authObj = response.json();
this.expirationDate = new Date();
this.expirationDate.setSeconds(this.expirationDate.getSeconds() + this.authObj.expires_in);
console.log(this.authObj, this.expirationDate);
},
error => console.log("Error: ", error),
function() {
console.log('resolving');
resolve();
});
});
// }
}
getfromgraphql() {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
// headers.append('Authorization', 'Bearer ' + this.access_token );
let options = new RequestOptions({ headers: headers });
this.checkAccessTokenValidity().then(function() {
console.log('in then');
//noinspection TypeScriptValidateTypes
return this.http.post(this.URL + '/api/v1/graphql', this.query, options);
});
}
尝试用以下内容替换您的post call:
return this.http.post(this.URL + '/api/v1/graphql', this.query, options).subscribe(() => {});
看看它现在是否工作。我不明白你为什么要这样做。http.getfromgraphql()但是为了其他原因 以反应式方式编码,如下所示:
checkAccessTokenValidity() {
let headers = new Headers();
headers.append( 'Content-Type', 'application/x-www-form-urlencoded' );
let options = new RequestOptions({ headers: headers });
return this.http
.post(this.URL + 'oauth/token', this.authentication, options)
.map(response => {
this.authObj = response.json();
this.expirationDate
.setSeconds((new Date())
.getSeconds() + this.authObj.expires_in);
});
}
getfromgraphql() {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
// headers.append('Authorization', 'Bearer ' + this.access_token );
let options = new RequestOptions({ headers: headers });
this.checkAccessTokenValidity()
.switchMap(token => this.http
.post(this.URL + '/api/v1/graphql', this.query, options)
}
你调用这个方法的方式是
someService.getfromgraphql()
.subscribe(response=> {
// ....
});
第1点:
不要在任何回调中使用function(){}
。假设您使用的是Typescript(或者ES6),那么您没有使用lexical
this。您的所有此
都将引用内部作用域。你会得到很多未定义的。使用粗箭头符号()=>{}
第2点:
由于您已经在所有方法中使用了可观察对象
,请继续使用它们(反应式编程),除非必要,否则不要使用Promise
在getfromgraphql()
方法中,只需使用flatMap()
。它与promise中的.then()
相同
此外,在解决错误时,如果需要,只需执行.catch()
。Observables将把错误传播给最高的调用方
checkAccessTokenValidity() {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({headers: headers});
return this.http.post(this.URL + 'oauth/token', this.authentication, options)
.map(response => {
this.authObj = response.json();
this.expirationDate = new Date();
this.expirationDate.setSeconds(this.expirationDate.getSeconds() + this.authObj.expires_in);
console.log(this.authObj, this.expirationDate);
})
.catch(error=>{
console.log(error);
return Observable.empty();
})
}
getfromgraphql() {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
// headers.append('Authorization', 'Bearer ' + this.access_token );
let options = new RequestOptions({headers: headers});
return this.checkAccessTokenValidity().flatMap(()=>{
console.log('in then');
//noinspection TypeScriptValidateTypes
return this.http.post(this.URL + '/api/v1/graphql', this.query, options);
})
}
用这个
return this.http.post(this.heroesUrl, { name }, options)
.toPromise()
或者在订阅中添加resolve()
.subscribe(function(response) {
this.authObj = response.json();
this.expirationDate = new Date();
this.expirationDate.setSeconds(this.expirationDate.getSeconds() +
this.authObj.expires_in);
console.log(this.authObj, this.expirationDate);
resolve();
},
您可以在错误正文中使用reject()来代替错误解析
下面的plunker将帮助您
检查app/toh文件夹中的hero.service.promise.ts如何使用函数
getfromgraphql()
?我使用这个.http.getfromgraphql()从某个组件调用getfromgraphql(),然后该函数从checkaccesstokenvalidity()获取一个新的访问令牌在这之后,它会向endpoint/api/v1/graphql发送一个post,但这不会触发。您能提供plunker吗?这给了我一个错误:类型“void”上不存在属性“subscribe”。调用this.http.getfromgraphql().subscribe(response=>{console.log(response);})@dennismuys抱歉,您需要返回可观察的对象。请看编辑,plunker似乎是空的