1
votes

Unde rmy Angular 6 app , i am trying use some GET request , while injecting some Headers :

My service looks like this :

@Injectable()
export class MyService {
constructor(public httpClient: HttpClient) {
}
getUserInfos(cuid): Observable<any> {
    const headers = new HttpHeaders({'login': login});
    return this.httpClient.get(environment.urls.UserHabilitation, {headers});
}

Within my Component : I'm subscribing to it like the following :

this.myService.getUserInfos(login).subscribe(infos => {
      console.log(infos );
      error => {
        console.log(error);
});

It works well under Firefox , but not under Chrome and IE11 ; and i got such ann error :

TypeError: Cannot read property 'length' of null at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate (http.js:199) at http.js:170 at Array.forEach () at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.init (http.js:170) at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.forEach (http.js:235) at Observable._subscribe (http.js:1445) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:42) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28) at subscribeTo.js:21 at subscribeToResult (subscribeToResult.js:6)

Suggestions ??

1

1 Answers

1
votes

It looks like your '}' is in the wrong place. Subscribe can get 2 arguments, the execution and the error response:

this.myService.getUserInfos(login).subscribe(
      infos => {
        console.log(infos);
      },
      error => {
        console.log(error);
      }
);

Or shorter:

this.myService.getUserInfos(login).subscribe(
      infos => console.log(infos ),
      error => console.log(error)
);