首页 > 解决方案 > 从 Cloud Firestore 中提取时,时间戳以字符串形式出现

问题描述

我已将时间戳数据提取到 html 数据属性中。

然后我想用它来填充将在模式弹出窗口上的日期表单字段。

但是,当尝试使用时,toDate()我收到了错误

Uncaught TypeError: moderationDate.toDate is not a function

为了测试,我使用警报来检查变量的值和数据类型moderationDate。我可以看到数据作为字符串存储在 html 数据属性中

<td data-moderateModerationDate="'+student.UnitGrades.IT6.Moderate.ModerationDate+'" >table data item</td> \

let moderationDate = $(this).attr("data-moderateModerationDate");

alert(moderationDate);
alert(typeof moderationDate);

谁能告诉我为什么这是一个字符串数据类型而不是原始时间戳?

标签: javascriptgoogle-cloud-firestore

解决方案


HTML data attributes can only store strings, so you can't expect to put anything into it and expect to get exactly that thing back. The browser is converting it to a string when you store it, as you are adding it using string concatenation. I'd suggest trying to find another way to store this value.


推荐阅读