Adding the 2nd timestamp shows this error failed to fetch data TypeError: Cannot read properties of undefined (reading 'seconds')

Adding the 2nd timestamp shows this error failed to fetch data TypeError: Cannot read properties of undefined (reading 'seconds')

如果我添加 editedAt,它会抛出这个错误:

failed to fetch data TypeError: Cannot read properties of undefined (reading 'seconds')

但是,如果我从数据库中删除时间戳值为 editedAt 的数据。这不会导致错误。这是因为并非所有数据的时间戳值都是 editedAt?

由于我还需要使用 editedAt 显示数据,因此如何解决此错误?

这是数据:

const data = [
  {
    createdDate: { seconds: 1647660530, nanoseconds: 228000000 },
    id: "4jnEbtIGLKTgEnJYX7ci"
  },
  {
    editedAt: { seconds: 1647665687, nanoseconds: 627000000 },
    id: "vBBeIQsNbqvbqVWNIK6k"
  },
  {
    createdDate: { seconds: 1646370332, nanoseconds: 678000000 },
    id: "Hs29gnvfeD0cSQkNmsRn"
  },
  {
    createdDate: { seconds: 1645840425, nanoseconds: 486000000 },
    id: "gagdiaihdihaihsd"
  }
];

这些是代码:

 const columns = [
    {
      name: "createdDate",
      label: "Date",
      options: {
        filter: true,
        sort: true,
        customBodyRender: (value, tableMeta, updateValue) => {
          return new Date(value.seconds * 1000).toLocaleDateString();
        },
      },
    },
    {
      name: "editedAt",
      label: "Edited Date",
      options: {
        filter: true,
        sort: true,
        customBodyRender: (value, tableMeta, updateValue) => {
          return new Date(value.seconds * 1000)?.toLocaleDateString();
        },
      },
    },
  ];

数据数组中的每一项都应包含字段“createdDate”和“editedAt”。

如果您不想这样做,请使用可选的 ?运算符不处理任何数据或检查值:

if (value) { ... do render } else { ... do something else }

value?.seconds 可以 return 未定义或访问秒数 属性.