我正在尝试创建一个observable,但我的代码失败了:
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
this._log.debug('SecureHttpService#get: ' + url);
let resultObservable = Observable.create((observer) => {
this._log.debug('resultObservable');
this.tryReActivateToken().then(
(result) => {
this._log.debug('resultObservable#then#result: ' + result);
if (result === true) {
let headers = new Headers();
headers.append('Authorization', 'Bearer ' + this.access_token);
headers.append('X-Requested-With', 'XMLHttpRequest');
// headers.append('Accept', 'json');
this._log.debug(this.access_token);
let superGetObs = super.get(url, { headers: headers, withCredentials: true });
superGetObs.subscribe(
(next) => { observer.onNext(next); },
(error) => { observer.onError(error); },
() => { observer.onCompleted(); }
);
} else {
observer.onError(new Error('Could not log you in automatically'));
}
}, (error) => { this._log.debug('resultObservable#then#error: ' + error); observer.onError(error); });
});
return resultObservable;
}
我得到的错误是:http://pastebin.com/BBcdc9sy
我做错了什么?
答案 0 :(得分:3)
在RxJs中,observer的方法被重命名。您应该使用error
代替onError
。
答案 1 :(得分:1)
我认为你写错了'onError'的语法。真的是'错误'。你可以尝试这个,我希望它能解决你的问题;
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
this._log.debug('SecureHttpService#get: ' + url);
let resultObservable = Observable.create((observer) => {
this._log.debug('resultObservable');
this.tryReActivateToken().then(
(result) => {
this._log.debug('resultObservable#then#result: ' + result);
if (result === true) {
let headers = new Headers();
headers.append('Authorization', 'Bearer ' + this.access_token);
headers.append('X-Requested-With', 'XMLHttpRequest');
// headers.append('Accept', 'json');
this._log.debug(this.access_token);
let superGetObs = super.get(url, { headers: headers, withCredentials: true });
superGetObs.subscribe(
(next) => { observer.onNext(next); },
(error) => { observer.error(error); },
() => { observer.onCompleted(); }
);
} else {
observer.onError(new Error('Could not log you in automatically'));
}
}, (error) => { this._log.debug('resultObservable#then#error: ' + error); observer.onError(error); });
});
return resultObservable;
}