私はobservableとAPI呼び出しを処理し、要求でauthトークンをラップするための共通のangular2サービスを実装するのが初めてです。私はそれを少し変更した401エラーを処理するために今すぐhttps://gist.github.com/chandermani/9166abe6e6608a31f471Angular2未定義のシンボル 'Symbol(Symbol.iterator)'を読み取ることができません
を使用していますそのために
。
private _request(method: RequestMethod, url: string, body?: any, options?: RequestOptionsArgs): Rx.Observable<Response> {
let requestOptions = new RequestOptions(Object.assign({
method: method,
url: url,
body: body
}, options));
if (!requestOptions.headers) {
requestOptions.headers = new Headers();
}
requestOptions.headers.set('Authorization', this._buildAuthHeader());
return Rx.Observable.create((observer) => {
this.process.next(Action.QueryStart);
this._http.request(new Request(requestOptions))
.map(res => res.json())
.finally(() => {
this.process.next(Action.QueryStop);
}).catch((error) => {
if (error.status === 401) {
this.renewToken().subscribe((res) => { }, (err) => { },() => {
requestOptions.headers.set('Authorization', this._buildAuthHeader());
this._http.request(new Request(requestOptions))
.map(res => res.json())
.subscribe(
(res) => {
observer.next(res);
observer.complete();
},
(err) => {
},
() => {
});
});
} else {
observer.next();
observer.complete();
return Rx.Observable.timer(1);
}
})
.subscribe(
(res) => {
observer.next(res);
observer.complete();
},
(err) => {
switch (err.status) {
case 401:
// intercept 401
this.authFailed.next(err);
observer.error(err);
break;
default:
observer.error(err);
break;
}
});
});
}
期待どおりに機能します。 401では、トークンをリフレッシュし、APIコールを実行し、APIから結果を返します。しかし、それはコンソールでエラーを出します。
Cannot read property 'Symbol(Symbol.iterator)' of undefined
TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
at Object.subscribeToResult (eval at <anonymous> (http://localhost:8080/js/vendor.js:48:1), <anonymous>:45:27)
at CatchSubscriber.error (eval at <anonymous> (http://localhost:8080/js/vendor.js:1921:1), <anonymous>:60:33)
at FinallySubscriber.Subscriber._error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:128:26)
at FinallySubscriber.Subscriber.error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:102:18)
at MapSubscriber.Subscriber._error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:128:26)
at MapSubscriber.Subscriber.error (eval at <anonymous> (http://localhost:8080/js/vendor.js:20:1), <anonymous>:102:18)
at XMLHttpRequest.onLoad (eval at <anonymous> (http://localhost:8080/js/vendor.js:1634:1), <anonymous>:88:34)
at ZoneDelegate.invokeTask (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:236:37)
at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (eval at <anonymous> (http://localhost:8080/js/vendor.js:2622:1), <anonymous>:35:37)
at ZoneDelegate.invokeTask (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:235:42)
at Zone.runTask (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:136:47)
at XMLHttpRequest.ZoneTask.invoke (eval at <anonymous> (http://localhost:8080/js/polyfills.js:759:1), <anonymous>:304:33)
私は問題またはその追跡方法を手伝ってください。