Angular HttpClient TypeError: Object(...) is not a function

2.2k views Asked by At

I had nebular admin panel version 5.0.0 using angular 9 and was working perfectly, I upgraded to version 6.0.0 which uses Angular 10 ( by upgrading the versions in package.json ), now in dev server everything is working perfectly, but after deploying to the production server ( apache on linux ) I get error calling a simple GET request using HttpClient.

TypeError: Object(...) is not a function
    at t.intercept (main-es2015.e7f064813bf8e3e8865e.js:1)
    at E.handle (main-es2015.e7f064813bf8e3e8865e.js:1)
    at t.intercept (main-es2015.e7f064813bf8e3e8865e.js:1)
    at E.handle (main-es2015.e7f064813bf8e3e8865e.js:1)
    at t.intercept (main-es2015.e7f064813bf8e3e8865e.js:1)

In app.component.ts

ngOnInit() {
 this.siteService.getAll().subscribe(properties => {
        console.log('out:' + properties);
      }, error => {
        console.log('error:' + error); // error is reported here
      });
}

site.service.ts

@Injectable({
  providedIn: 'root',
})
export class SiteService extends AbstractService {
  constructor(protected httpUtils: HttpUtils) {
    super(httpUtils);
  }

  public getAll(): Observable<SiteProperty[]> {
    const url = AbstractService.baseUrl + 'api/properties';

    return this.httpUtils.get<SiteProperty[]>(url);

  }
}

HttpUtil.ts

@Injectable({
    providedIn: 'root',
})
export class HttpUtils {

    headers = new HttpHeaders({'Content-type': 'application/json', 'Accept': 'application/json'});
    constructor(protected httpClient: HttpClient) {}

    post<T>(url: string, object: any): Observable<T> {
        return this.httpClient.post<T>(url, object, {headers: this.headers, withCredentials: true});
    }

    put<T>(url: string, object: any): Observable<T> {
        return this.httpClient.put<T>(url, object, {headers: this.headers, withCredentials: true});
    }

    delete<T>(url: string): Observable<T> {
        return this.httpClient.delete<T>(url, {headers: this.headers, withCredentials: true});
    }

    get<T>(url: string, params?: HttpParams): Observable<T> {
        console.log('get:' + url);
        const respo = this.httpClient.get<T>(url, {headers: this.headers, params: params, withCredentials: true});
        console.log('done get:' + url);
        return respo;
    }
}

error caused in intercept, throwError(error) see below

httpinterceptor.ts

import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {catchError} from 'rxjs/operators/catchError';
import { throwError } from 'rxjs/internal/observable/throwError';

@Injectable()
export class GlobalHttpInterceptorService implements HttpInterceptor {
    constructor() {}

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

          return next.handle(req).pipe(
             catchError( (error) => {
              console.log(error);
              return throwError(error);
          }),
        );
    }
}

package.json

{
  "name": "ngx-admin",
  "version": "5.0.0",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/akveo/ngx-admin.git"
  },
  "bugs": {
    "url": "https://github.com/akveo/ngx-admin/issues"
  },
  "scripts": {
    "ng": "ng",
    "conventional-changelog": "conventional-changelog",
    "start": "ng serve --host 0.0.0.0 --disable-host-check",
    "build": "ng build",
    "build:prod": "npm run build -- --prod --aot",
    "test": "ng test",
    "test:coverage": "rimraf coverage && npm run test -- --code-coverage",
    "lint": "ng lint",
    "lint:fix": "ng lint ngx-admin-demo --fix",
    "lint:styles": "stylelint ./src/**/*.scss",
    "lint:ci": "npm run lint && npm run lint:styles",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "ng e2e",
    "docs": "compodoc -p src/tsconfig.app.json -d docs",
    "docs:serve": "compodoc -p src/tsconfig.app.json -d docs -s",
    "prepush": "npm run lint:ci",
    "release:changelog": "npm run conventional-changelog -- -p angular -i CHANGELOG.md -s",
    "postinstall": "ngcc --properties es2015 es5 browser module main --first-only --create-ivy-entry-points"
  },
  "dependencies": {
    "@akveo/ng2-completer": "^9.0.1",
    "@angular/animations": "^10.0.10",
    "@angular/cdk": "^10.1.1",
    "@angular/common": "^10.0.10",
    "@angular/compiler": "^10.0.10",
    "@angular/core": "^10.0.10",
    "@angular/forms": "^10.0.10",
    "@angular/google-maps": "^10.1.3",
    "@angular/localize": "^11.0.4",
    "@angular/material": "^10.0.10",
    "@angular/platform-browser": "^10.0.10",
    "@angular/platform-browser-dynamic": "^10.0.10",
    "@angular/router": "^10.0.10",
    "@auth0/angular-jwt": "^3.0.0",
    "@ncstate/sat-popover": "^3.2.0",
    "@nebular/auth": "6.0.0",
    "@nebular/eva-icons": "6.0.0",
    "@nebular/security": "6.0.0",
    "@nebular/theme": "6.0.0",
    "@ng-bootstrap/ng-bootstrap": "^8.0.0",
    "angular2-chartjs": "0.4.1",
    "bootstrap": "4.3.1",
    "chart.js": "2.7.1",
    "classlist.js": "1.1.20150312",
    "core-js": "2.5.1",
    "crypto-js": "^3.1.9-1",
    "eva-icons": "^1.1.3",
    "font-awesome": "^4.7.0",
    "intl": "1.2.5",
    "ionicons": "2.0.1",
    "nebular-icons": "1.1.0",
    "ng2-completer": "^9.0.1",
    "ng2-smart-table": "^1.6.0",
    "node-sass": "^4.12.0",
    "normalize.css": "6.0.0",
    "pace-js": "1.0.2",
    "roboto-fontface": "0.8.0",
    "rxjs": "6.6.2",
    "rxjs-compat": "6.3.0",
    "socicon": "3.0.5",
    "style-loader": "^1.1.3",
    "tinymce": "4.5.7",
    "tslib": "^2.0.0",
    "typeface-exo": "0.0.22",
    "web-animations-js": "^2.3.2",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.6",
    "@angular/cli": "^10.0.6",
    "@angular/compiler-cli": "^10.0.6",
    "@angular/language-service": "10.0.10",
    "@compodoc/compodoc": "1.0.1",
    "@fortawesome/fontawesome-free": "^5.2.0",
    "@types/d3-color": "1.0.5",
    "@types/jasmine": "2.5.54",
    "@types/jasminewd2": "2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "conventional-changelog-cli": "1.3.4",
    "husky": "0.13.3",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "npm-run-all": "4.0.2",
    "protractor": "~7.0.0",
    "rimraf": "2.6.1",
    "stylelint": "7.13.0",
    "ts-node": "3.2.2",
    "tslint": "~6.1.0",
    "tslint-language-service": "^0.9.9",
    "typescript": "3.9.7",
    "@types/crypto-js": "^3.1.43"
  }
}
1

There are 1 answers

4
sami On BEST ANSWER

I had it working by fixing the imports in httpinterceptor.ts

before

import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {catchError} from 'rxjs/operators/catchError';
import { throwError } from 'rxjs/internal/observable/throwError';

after

import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';