Firebase Firestore将时间戳记返回为空对象

时间:2019-04-13 04:28:16

标签: firebase vuejs2 google-cloud-firestore

firebase.firestore().collection('items')

按预期返回所有字符串,但是在时间戳创建的字符串将作为空对象{}

返回
{"created_at":{},"name":"item one","description":"item one desc"}

如何获取它提供在Firebase控制台的数据库中显示的实际时间戳?

As it appears in firebase console

从以上集合返回的结果存储为items,并显示在vue js的表中。

1 个答案:

答案 0 :(得分:2)

Firebase Firestore时间戳是一个对象,如果要显示它,则必须将其转换为js日期对象。您可以read more here in the public constructors section.

示例js:

firebase
    .firestore()
    .collection('items')
    .get()
    .then((querySnapshot) => {
      const data = [];
      querySnapshot.forEach((item) => {
        const { created_at, ...rest } = item.data();

        data.push({
          created_at: new Date(created_at.seconds * 1000), // or you can use moment - moment.unix(created_at.seconds)
          ...rest,
        });
      });

      console.log(data); // [{"created_at": "2019-04-10T15:25:04.000Z", "name": "item one", "description": "item one desc"}]
    });