Javascript 如何在Firestore.FieldValue.Timestamp上使用toDate()?

Javascript 如何在Firestore.FieldValue.Timestamp上使用toDate()?,javascript,google-cloud-firestore,timestamp,Javascript,Google Cloud Firestore,Timestamp,我试图从firestore数据库填充一个表,该数据库具有字段值时间戳数据: 使用以下web代码: firebase.initializeApp(firebaseConfig); const attendanceCollection = firebase.firestore().collection("attendance"); var lastIndex = 0; function getData() { var htmls = []; attendanceCollect

我试图从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.initializeApp(firebaseConfig);
const attendanceCollection=firebase.firestore().collection(“出席”);
var lastIndex=0;
函数getData(){
var htmls=[];
attendanceCollection.get().then(querySnapshot=>{
querySnapshot.forEach(doc=>{
log(`${doc.id}`);
console.log(`${(doc.data().generalasemply)}`)
htmls.unshift('\
'+`${doc.id}`+'\
'+`${(doc.data().generalasemply)}`+'\
'+`${doc.id}`+'\
');
});
$('#tbody').html(htmls);
$(“#submitUser”).removeClass('desaled');
});
};
其中返回以下内容:

我找到了一些参考资料,但从未真正帮助我解决我想做的事情:


如何在上面的代码中实现toDate()?您能给我一些将firestore时间戳转换为日期/字符串时使用toDate()的示例吗?

您需要在
GENERALASSEMBLY
字段上调用
toDate()
,以获取其作为日期的值。而且,由于当第一个事件触发时,字段最初将为
null
,因此您还需要检测
generalasemply
是否为
null
first

${(doc.data().GENERALASSEMBLY ? doc.data().GENERALASSEMBLY.toDate() : "-unknown-")}

您需要在
GENERALASSEMBLY
字段上调用
toDate()
,以获取其作为日期的值。而且,由于当第一个事件触发时,字段最初将为
null
,因此您还需要检测
generalasemply
是否为
null
first

${(doc.data().GENERALASSEMBLY ? doc.data().GENERALASSEMBLY.toDate() : "-unknown-")}

如果您使用的是库日期fns,请记住现在是unix时间
fromUnixTime(generalasemply.seconds)

然后您可以使用
格式
并解析fromUnixTime


格式(fromUnixTime(generalasemply.seconds))

如果使用库日期fns,请记住现在是unix时间
fromUnixTime(generalasemply.seconds)

然后您可以使用
格式
并解析fromUnixTime


format(fromUnixTime(generalasemply.seconds))

您是否尝试在
doc.data().generalasemply
上调用
toDate()
。它返回一个错误,您从doc.data().GENERALASSEMBLY的控制台日志中得到了什么?它是一个具有秒和纳秒属性的对象吗?@chrismlake是的,正如您在
doc.data().generalasembly
上尝试调用
toDate()
的第二个PhotoID所看到的那样。它返回一个错误,您从doc.data().GENERALASSEMBLY的控制台日志中得到了什么?它是一个具有秒和纳秒属性的物体吗?@chrismclarke是的,正如在第二张照片上看到的,谢谢!我可以知道:“-uknown-”是为了什么吗?它只是在没有日期时打印的内容。你可以用任何适合你的用例的东西来替换它。谢谢!我可以知道:“-uknown-”是为了什么吗?它只是在没有日期时打印的内容。你可以用任何适合你的用例的东西来代替它。