I am attempting to loop through the JSON data and extract the start time and end time keys. I have tried two different methods in my API code to achieve this. The console.log is not showing any errors, but the other loop method is causing an error to appear in the console.
Here is a snippet of my show-api.ts code:
import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
interface Employee {
Id: String;
EmployeeName: String;
StartTimeUtc: String;
EndTimeUtc: String;
EntryNotes: String;
DeletedOn: String;
}
@Component({
selector: 'app-show-api',
templateUrl: './show-api.component.html',
styleUrls: ['./show-api.component.css']
})
export class ShowApiComponent implements OnInit {
li:any;
lis: any=[];
constructor(private http : HttpClient){
}
ngOnInit(): void {
this.http.get('Api of my JSON file')
.subscribe(Response => {
if(Response){
hideloader();
}
const employee: Employee[]=Response as Employee[];
console.log(employee[1].EndTimeUtc.valueOf()) // No error displayed
for(var i=0;i<1;i++)
{
const date1inString=(employee[i].EndTimeUtc.valueOf()); // Error displayed here.
const date2inString=(employee[i].StartTimeUtc.valueOf()); // Another error displayed here.
console.log(date1inString);
console.log(date2inString);
}
});
function hideloader(){
document.getElementById('loading')!.style.display = 'none';
}
}
The console.log is not displaying any errors when showing the date correctly. However, saving it in a variable results in the following error being shown in the console:
core.mjs:6485 ERROR TypeError: Cannot read properties of undefined (reading 'valueOf')
at Object.next (show-api.component.ts:51:48)
at ConsumerObserver.next (Subscriber.js:91:1)
at SafeSubscriber._next (Subscriber.js:60:1)
at SafeSubscriber.next (Subscriber.js:31:1)
at map.js:7:1
at OperatorSubscriber._next (OperatorSubscriber.js:13:1)
at OperatorSubscriber.next (Subscriber.js:31:1)
at filter.js:6:50
at OperatorSubscriber._next (OperatorSubscriber.js:13:1)
at OperatorSubscriber.next (Subscriber.js:31:1)
Data received from the API:
[
{"Id": "aa",
"EmployeeName": "bb",
"StarTimeUtc": "cc",
"EndTimeUtc": "dd",
"EntryNotes": "ee",
"DeletedOn": null },
{"Id": "ff",
"EmployeeName": "gg",
"StarTimeUtc": "hh",
"EndTimeUtc": "ii",
"EntryNotes": "jj",
"DeletedOn": null },
]