Skip to content
Advertisement

How to save date as Timestamp in firestore using firebase-admin?

Trying to convert from javascript date to firestore timestamp throws

TypeError: Cannot read property 'Timestamp' of undefined

I tried it in 2 ways:

  1. Using firebase-admin
const admin = require('firebase-admin');
const db = admin.firestore();
const timestamp = db.Timestamp.fromDate(new Date(date));
  1. Using firebase.firestore:
const firebase = require('firebase');
const timestamp = firebase.firestore.Timestamp.fromDate(new Date(date));

date used as a param in new Date is of this format: “2017-01-29”.

Expected result: firestore timestamp.

Actual result: TypeError: Cannot read property ‘Timestamp’ of undefined

Note: db nor firebase are null or undefined.

Is there a definitive way of creating firestore timestamp from javascript date object?

Answer

Your second example isn’t using the Firebase Admin SDK at all – that’s the Firebase web client SDK.

If you’re working with the Admin SDK:

const admin = require('firebase-admin');
const timestamp = admin.firestore.Timestamp.fromDate(...)
Advertisement