I am working on a project which will need to migrate all instances of the old Http to utilize the new HttpClient provided by Angular. But I am having issues with the migration of one of my methods. I get this error ERROR TypeError: Cannot read properties of null (reading 'text') at main.js
Old Method:
ngAfterViewInit(): void {
var header = this.header;
if (header == null)
return;
var params = new URLSearchParams();
params.set('manufacturerCode', `${header.manufacturerCode || 0}`);
params.set('typeCode', `${header.typeCode || 0}`);
params.set('typeRevisionCode', `${header.typeRevisionCode || 0}`);
params.set('protocol', `${header.protocol || 0}`);
params.set('serialNumber', header.serialNumber);
params.set('tag', header.tag);
this.http.get(`./api/devices/device-icon`, { search: params }).pipe(map((response: Response) => response.text())).subscribe(data => {
let source: string;
if (data != null && data.length > 0)
source = `data:image/x-icon;base64,${data}`;
else
source = './assets/Default_Device_Icon.ico'; // use the default device icon
this.renderer.setElementAttribute(this.elementRef.nativeElement, 'src', source);
this.invisible = false;
});
}
New Method:
ngAfterViewInit(): void {
var header = this.header;
if (header == null)
return;
var params = new HttpParams();
params.set('manufacturerCode', `${header.manufacturerCode || 0}`);
params.set('typeCode', `${header.typeCode || 0}`);
params.set('typeRevisionCode', `${header.typeRevisionCode || 0}`);
params.set('protocol', `${header.protocol || 0}`);
params.set('serialNumber', header.serialNumber);
params.set('tag', header.tag);
this.httpClient.get(`./api/devices/device-icon`, { params, responseType: 'text'})
.pipe(map((response: any) => response.text()))
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(async data => {
let source: string;
if (data != null && (await data).length > 0)
source = `data:image/x-icon;base64,${data}`;
else
source = './assets/Default_Device_Icon.ico'; // use the default device icon
this.renderer.setAttribute(this.elementRef.nativeElement, 'src', source);
this.invisible = false;
});
}