错误类型错误:无法读取未定义的属性(读取“valueOf”)
我正在尝试迭代 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';}
}}
控制台数据中显示以下错误
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)
console.log 没有显示错误并正确显示日期,但是当我将其保存在变量中时,它在来自 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 },
]
I am trying to iterate through the JSON data and access the key of start time and end time.
In my API code, I have tried 2 ways to do this.
Console.log is not showing error
but the other loop method is showing an error in the console.
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())//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';}
}}
The console.log is not showing an error and displaying the date properly but when I save it in a variable it is showing the following error 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 from 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 },
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
API 的响应数据没有 StartTimeUtc。因此,您尝试读取未定义的 valueof() ,这会导致此错误。
这是一个拼写错误 StarTimeUtc 而不是 StartTimeUtc
Response Data from API does not have StartTimeUtc. So you are trying to read valueof() of undefined which is giving you this error.
Its a typo StarTimeUtc instead of StartTimeUtc
检查你的 for 循环。我认为这个条件不对。
使用这个,并让我知道
Check your for loop. I think it's wrong the condition.
Use this, and let me know