[英]Can't use Firestore timestamp('toDate' of undefined)
[英]How to use toDate() on Firestore.FieldValue.Timestamp?
我正在尝试从具有字段值时间戳数据的Firestore数据库填充表:
使用以下用于Web的代码:
firebase.initializeApp(firebaseConfig);
const attendanceCollection = firebase.firestore().collection("attendance");
var lastIndex = 0;
function getData() {
var htmls = [];
attendanceCollection.get().then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(`${doc.id}`);
console.log(`${(doc.data().GENERALASSEMBLY)}`)
htmls.unshift('<tr>\
<td>'+ `${doc.id}` +'</td>\
<td class="message" style=" text-overflow: ellipsis">'+ `${(doc.data().GENERALASSEMBLY)}` +'</td>\
<td style="overflow: hidden; text-overflow: ellipsis">'+ `${doc.id}` +'</td>\
</tr>');
});
$('#tbody').html(htmls);
$("#submitUser").removeClass('desabled');
});
};
我找到了一些参考资料,但实际上并没有真正帮助我解决我要做的事情:
如何从新的firebase.firestore.FieldValue.serverTimestamp()获取JavaScript Date对象
如何将Firestore日期/时间戳转换为JS Date()?
如何在上面的代码中实现toDate()? 你能给我示例使用toDate()将Firestore时间戳转换为日期/字符串吗?
您需要在GENERALASSEMBLY
字段上调用toDate()
以获取其值作为日期。 并且由于在第一个事件触发时该字段最初将为null
,因此您还需GENERALASSEMBLY
检测GENERALASSEMBLY
是否为null
。
${(doc.data().GENERALASSEMBLY ? doc.data().GENERALASSEMBLY.toDate() : "-unknown-")}
如果您使用的是date- fromUnixTime(GENERALASSEMBLY.seconds)
库,请记住它是unix时间,所以fromUnixTime(GENERALASSEMBLY.seconds)
然后您可以使用format
并解析fromUnixTime
format(fromUnixTime(GENERALASSEMBLY.seconds))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.