Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 : HttpHeaders Cannot read property 'length' of null in Chrome and IE11 (and not in Firefox)

Under my Angular6 app , i'm using HttpClient with some headers injection to my htpp calls to fetch data from my backend server :

My service :

@Injectable()
export class LoadUserInfosService {

  public _headers = new HttpHeaders().set('Content-Type', 'application/json');

  constructor(private httpClient: HttpClient) {}

  getUserPnsInfos(cuid): Observable<object> {
    const headers = this._headers.append('login', login);
    return this.httpClient.get(myBackUrl, {headers: headers});
  }
}

My component where i'm subscribing to it :

loadUserInfosFromPns(login) {
    this.loadUserInfosService.getUserPnsInfos(login).subscribe(infos => {
      let receivedInfos: any;
        receivedPnsInfos = infos ;
        console.log(receivedPnsInfos);
        if (pnsInfos !== null && receivedPnsInfos.cuid === cuid ) {
        } else {
          this.router.navigate(['unauthorized'], {skipLocationChange: true});
        }
      },
      error => {
        console.log(error);
      });
  }

when running on Chrome or IE11 i'm getting some error refering to my request headers:

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 (<anonymous>)
    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.subscribe (Observable.js:161)
    at subscribeTo.js:21
    at subscribeToResult (subscribeToResult.js:6)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._innerSub (mergeMap.js:127)

this error appears only in Chrome and IE11 - while under Firefox the request get well and i'm not getting it

Any ideas , suggestions ?

like image 279
firasKoubaa Avatar asked Jul 13 '18 11:07

firasKoubaa


1 Answers

It is probably caused because you are settting an undefined / null header in some http request.

like image 80
Nikos Tsokos Avatar answered Oct 22 '22 12:10

Nikos Tsokos