ShadowZzz
ShadowZzz

Reputation: 415

Firebase Firestore Returning Timestamp as Empty Object

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

Returns all strings as expected, but the created at timestamp is returned as an empty object {}

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

How can I get it to provide the actual timestamp that appears in the db in the firebase console?

As it appears in firebase console

The result returned from the above collection is stored as items and is displayed in a table in vue js.

Upvotes: 2

Views: 785

Answers (1)

ccoeder
ccoeder

Reputation: 265

Firebase Firestore Timestamp is an object, if you want to display it, you have to convert it into a js date object. You can read more here in the public constructors section.

Example 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"}]
    });

Upvotes: 2

Related Questions