I encountered an error message while making a http request in my Angular Service. Strangely, this error only occurs after I logout, but it disappears upon logging back in:
Below is the code snippet of my authentication Service:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, retry, map } from 'rxjs/operators';
import { JwtHelperService } from '@auth0/angular-jwt';
export interface ServerResponse {
success: boolean;
msg: string;
user: any;
token: any
}
@Injectable({
providedIn: 'root'
})
export class AuthService {
authToken: any;
user: any;
public baseUri: string = 'http://localhost:3000/users';
public headers = new HttpHeaders().set('Content-Type', 'application/json')
constructor(private http: HttpClient) { }
public registerUser(user): Observable<ServerResponse> {
return this.http.post<ServerResponse>(this.baseUri + '/register', user, { headers: this.headers });
}
...
This is how the logout function in my component is implemented:
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../Services/auth.service';
import { PostService } from "../Services/post.service";
import { Router } from '@angular/router';
import { FlashMessagesService } from 'angular2-flash-messages';
//declare var anime: any;
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
...
onLogoutClick() {
this.authService.logout();
this.flashMessage.show('You are logged out', {cssClass: 'alert-success', timeout: 2000});
this.router.navigate(['login']);
return false;
}
}
The error logs point to the following issue:
core.js:6456 ERROR TypeError: Cannot read properties of null (reading 'length')
at HttpHeaders.applyUpdate (http.js:236)
at http.js:208
...
Despite everything seeming to work fine, I am still puzzled by this error. Any insight or assistance would be greatly appreciated. Thank you.