JavaEar 专注于收集分享传播有价值的技术资料

Angular2 Cannot read property 'Symbol(Symbol.iterator)' of undefined

I am new to observable and implementing common angular2 service to handle API calls and to wrap auth token with request.

For that I am using https://gist.github.com/chandermani/9166abe6e6608a31f471

Now for handling 401 error I have changed it little bit.

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;
                }
            });
    });
}

Which works fine as expected. On 401 it refreshes token and then execute API call and return result from API. But it gives error in console.

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)

Please help me with the problem or how to trace it.

1个回答

    最佳答案