问题
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 ?
回答1:
It is probably caused because you are settting an undefined / null header in some http request.
来源:https://stackoverflow.com/questions/51323979/angular-6-httpheaders-cannot-read-property-length-of-null-in-chrome-and-ie11