You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS代码运行报错Cannot read properties of undefined (reading 'seconds')如何修复

问题原因

你声明的names是数组类型,而非普通对象,数组本身不存在date属性,所以names.date的返回值为undefined,后续尝试读取undefined.seconds就会触发你遇到的报错。

修复方法

先通过数组下标访问到你需要读取的数组成员对象,再读取该对象下的date属性即可,示例如下:

const names = [
    {
      displayName: "anna",
      date: { seconds: 1630135912, nanoseconds: 920000000 }
    }
  ];

// 取下标为0的第一个数组成员,再读取date属性
console.log(new Date(names[0].date.seconds * 1000).toDateString());

如果需要批量处理数组内所有对象的日期转换,可以用遍历方法实现:

names.forEach(item => {
  console.log(`${item.displayName}对应的日期为:${new Date(item.date.seconds * 1000).toDateString()}`)
})

内容的提问来源于stack exchange,提问作者JS3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 08:36:05