7

I am calling an API endpoint and receiving an error back but I can not view the error when using console.log because I get error below. Is there another way to get the error?

ERROR

TypeError: Cannot read property 'length' of null
    at http.js:109
    at Array.forEach (<anonymous>)
    at HttpHeaders.lazyInit (http.js:103)
    at HttpHeaders.init (http.js:251)
    at HttpHeaders.forEach (http.js:354)
    at Observable._subscribe (http.js:2153)
    at Observable._trySubscribe (Observable.js:172)
    at Observable.subscribe (Observable.js:160)
    at Object.subscribeToResult (subscribeToResult.js:23)
    at MergeMapSubscriber._innerSub (mergeMap.js:132)

PROVIDER

    import { HttpClient } from '@angular/common/http';
    import { Http, Headers , RequestOptions } from '@angular/http';
    import { Injectable } from '@angular/core';

    @Injectable()
    export class GamesProvider {

    // API url 
    apiUrl = 'http://xxxxx.com/api';

    headers : any;
    options: any;

    this.headers =  new Headers();
    this.headers.append('Accept', 'application/json');
    this.headers.append('Content-Type', 'application/json' );

    this.headers.append('Authorization', 'Bearer' + this.token);

    this.options = new RequestOptions({headers: this.headers});

    getUsers(ex) {
      return new Promise(resolve => {
       this.http.get(this.apiUrl+'/api/online/'+ex, {headers: this.options}).subscribe(data => {
        resolve(data);
       }, err => {
        console.log(err);
       }).catch((err) => {

                // Do messaging and error handling here

                return Observable.throw(err)
            });
     });
    }
12
  • Where do you use length? i think you miss the part where you use length property
    – carton
    Commented May 7, 2018 at 12:13
  • I dont use it... Commented May 7, 2018 at 12:17
  • Is this a function ? Can you show more of your code?
    – carton
    Commented May 7, 2018 at 12:24
  • Yes its the entire code of 1 function Commented May 7, 2018 at 12:25
  • return new Promise((resolve,reject) => and reject(err); inside of your catch or err=>{ reject(err);} I am not sure it may give the same result Commented May 7, 2018 at 12:34

2 Answers 2

23

The cause of this error is that you are adding a header to you http request that has a value of null. You aren't doing it in your provided code, so chances are that this happens in your http interceptor.

It could very easily happen if your http interceptor adds a token to every http request, even if a value for the token has not been set, or if the interceptor is not properly accessing the token variable.

1
  • Actually true for my issue. Seems like there is currently a problem in headers.set as it does not replace the value of the header with the existing key and just works as headers.append. Leaving me with two headers with the same keys, one with null value and the second with the newly set value. Commented Jan 14, 2019 at 6:14
10

This happens when any of the value in headers is null or undefined.

For example:

{ headers: this.options }

If this.options is something like:

{ token : null }

You will get the error mentioned in this question.
You won't even see a request being generated by angular http in this case.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.