user6408649
user6408649

Reputation: 1305

SyntaxError: Unexpected end of input in Angular 5 service

I know that it is a common problem, but I still don't understand why it is reproduced in my case. Сouldn't find an answer in a few days.

Folowing a part of the Angular service:

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Response, RequestOptionsArgs, ResponseContentType } from '@angular/http';
import { Http, RequestOptions, Request, RequestMethod, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';

@Injectable()
export class BaseService {
    private headers: Headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8', 'Data-Type': 'json' });

    constructor(private http: Http, private router: Router, ) {
    }

    public get<T>(url: string, options?: RequestOptionsArgs): Promise<T> {
        return this.http.get(url, options)
            .toPromise()
            .then(response => response.json() as T)
            .catch(this.handleError.bind(this));
    }

    public handleError(error: any) {
        if (error.status === 400) {
            switch (error.statusText) {
                case 'Authentication':
                    this.router.navigate(['/error/auth']);
                    break;
                default:
                    return Promise.reject(error);
            }
            return;
        }
        return Promise.reject(error.toString());
    }
}

Folowing method called base method:

getSearchResultTable(hash: number): Promise<Prices[]> {
    const tmp = this.baseService.get<Prices[]>(this.apiUrl + hash);
     return tmp;
}

Json that's returned from server is valid. I checked it one some online service.

But I see SyntaxError: Unexpected end of input error in Response when trying to debug.

Call Stack:

SyntaxError: Unexpected end of input
    at BaseService.get (webpack-internal:///../../../../../src/app/search-module/services/base.service.ts:36:14)
    at SearchResultService.getSearchResultTable (webpack-internal:///../../../../../src/app/search-module/services/search-result.service.ts:30:36)
    at SearchResultPageComponent.ngOnInit (webpack-internal:///../../../../../src/app/search-module/pages/search-result/search-result.component.ts:24:34)
    at checkAndUpdateDirectiveInline (webpack-internal:///../../../core/esm5/core.js:12291:19)
    at checkAndUpdateNodeInline (webpack-internal:///../../../core/esm5/core.js:13794:20)
    at checkAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:13737:16)
    at debugCheckAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:14609:76)
    at debugCheckDirectivesFn (webpack-internal:///../../../core/esm5/core.js:14550:13)
    at Object.eval [as updateDirectives] (ng:///SearchModule/SearchResultPageComponent_Host.ngfactory.js:9:5)
    at Object.debugUpdateDirectives [as updateDirectives] (webpack-internal:///../../../core/esm5/core.js:14535:21)

I'm using latest version of Angular.

Upvotes: 1

Views: 11536

Answers (1)

Nimish goel
Nimish goel

Reputation: 2761

From angular latest update:

remove any map(res => res.json()) calls, which are no longer needed.

So try to remove .json() from .then(response => response.json() as T).

Upvotes: 1

Related Questions