0

I am trying to use HttpClient in angular 6 in my service as follows:

auth.service.ts

constructor(private http: HttpClient, public dataService: DataService, private config: AppConfig) {
        console.log(this.config);
    }

This http:HttpClient is the reason I am getting the following error. When I remove this, the error goes. I am not sure why this error is coming.

core.js:1521 ERROR TypeError: Cannot read property 'length' of null
    at http.js:108
    at Array.forEach (<anonymous>)
    at HttpHeaders.lazyInit (http.js:102)
    at HttpHeaders.init (http.js:166)
    at HttpHeaders.forEach (http.js:235)
    at Observable._subscribe (http.js:1445)
    at Observable._trySubscribe (Observable.js:42)
    at Observable.subscribe (Observable.js:28)
    at subscribeTo.js:21
    at subscribeToResult (subscribeToResult.js:6)
6
  • 1
    Can you add your TS Import line ? Commented Jun 7, 2019 at 19:40
  • Do you mean the service import? I am declaring it in providers in app.module.ts. Commented Jun 7, 2019 at 19:43
  • Your TS import. It's to be sure you don't have import { HttpClient } from 'selenium-webdriver/http'; Commented Jun 7, 2019 at 19:45
  • No, it is this one - import { HttpClient, HttpParams, HttpRequest } from "@angular/common/http"; Commented Jun 7, 2019 at 19:46
  • And you don't have to put it in your provider but you have to use HttpClientModule Commented Jun 7, 2019 at 19:46

2 Answers 2

1

In Your app.module.ts:

import HttpClientModule, under imports array:

import { HttpClientModule } from '@angular/common/http';


@NgModule({
   imports: [ ..., HttpClientModule, ...]  // import here

And now you can inject HttpClient in your service:

import { HttpClient } from '@angular/common/http';

 ...............
 constructor(private http: HttpClient, public dataService: DataService, private 
 config: AppConfig) {
       console.log(this.config);
  }

Make sure your service is Injectable and provided in 'root' or in the AppModule providers.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not actually HttpClient.

You call

console.log(this.config);

in the constructor, but pass config as a parameter to constructor().

Passing parameters to the constructor() doesn't automatically set their similarly-named this. counterpart.

You probably want

console.dir(config);

or

this.config = config;
console.dir(this.config);

(Also, use console.dir() for objects.)

3 Comments

Thanks. But even after removing this console.log statement, I am getting the same error. I think it has something to do with httpClient as I am getting error in http.js
@Coder You're causing an exception to be thrown in the constructor, so other things later will likely fail. Is there more code in the service, or just that? If so please update the question.
When the parameter in the constructor is declared as public or private it is added to this. and is it also accessible directly. Without the public or private it works as you are saying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.