错误类型错误:无法读取未定义的属性(读取“valueOf”)

发布于 2025-01-11 04:14:35 字数 2260 浏览 0 评论 0原文

我正在尝试迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人接听 2025-01-18 04:14:35

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

习惯成性 2025-01-18 04:14:35

检查你的 for 循环。我认为这个条件不对。

使用这个,并让我知道

for (let i = 0; i < employee.length; i++)

Check your for loop. I think it's wrong the condition.

Use this, and let me know

for (let i = 0; i < employee.length; i++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文