我正在嘗試遍歷 JSON 資料并訪問開始時間和結束時間的鍵。在我的 API 代碼中,我嘗試了兩種方法來做到這一點。Console.log 未顯示錯誤,但其他回圈方法在控制臺中顯示錯誤。
我的 show-api.ts 代碼:-
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())//it is not showing error
for(var i=0;i<1;i )
{
const date1inString=(employee[i].EndTimeUtc.valueOf());//it is showing error
const date2inString=(employee[i].StartTimeUtc.valueOf());//it is showing error
console.log(date1inString);
console.log(date2inString);
}
});
function hideloader(){
document.getElementById('loading')!.style.display = 'none';}
}}
console.log 沒有顯示錯誤并正確顯示日期,但是當我將其保存在變數中時,它在控制臺中顯示以下錯誤
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)
來自 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 },
]
uj5u.com熱心網友回復:
來自 API 的回應資料沒有StartTimeUtc。因此,您正在嘗試讀取未定義的 valueof() ,這會給您帶來此錯誤。
它是一個錯字 StarTimeUtc 而不是 StartTimeUtc
uj5u.com熱心網友回復:
檢查你的 for 回圈。我認為這是錯誤的條件。
用這個,讓我知道
for (let i = 0; i < employee.length; i )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/436225.html
下一篇:如何進行日期選擇器自定義?
