【问题标题】:Cannot read properties of undefined (reading 'seconds')无法读取未定义的属性(读取“秒”)
【发布时间】:2021-11-20 06:07:54
【问题描述】:

我有这个name 对象,它说无法读取未定义的属性(读取“秒”)。我该如何解决这个问题?

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

  console.log(new Date(names.date.seconds * 1000).toDateString());

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    names 是一个数组,而不是一个对象。您需要对其进行索引以检索数组中的第一个对象,然后获取date 属性:names[0].date.seconds

    【讨论】:

      【解决方案2】:

      试试这个 console.log(new Date(names[0].date.seconds * 1000).toDateString());

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 2022-01-15
        • 2021-12-31
        • 2021-12-13
        • 2021-11-28
        • 2021-11-15
        • 2021-11-06
        • 2021-11-16
        • 2022-01-26
        相关资源
        最近更新 更多