Ad

How To Add Timestamp In Firebase Cloud Functions

I am trying to add Timestamp in Firestore document on Firebase Cloud functions.

I had tried firestore.Timestamp.fromDate(new Date()), but its not working.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firestore = admin.firestore();
const createdAt = firestore.Timestamp.fromDate(new Date());
console.log(createdAt);

Value of createdAt should return the server timestamp, but its throwing error.

Ad

Answer

Instead of this:

const firestore = admin.firestore();
const createdAt = firestore.Timestamp.fromDate(new Date());

Try this:

const firestore = admin.firestore;
const createdAt = firestore.Timestamp.fromDate(new Date());

Might be hard to spot the difference. So here is the explanation: You are calling the function "firestore()" of the object "admin" instead of accessing the property "firestore" of the object "admin" which has the property "Timestamp" you are looking for.

Ad
source: stackoverflow.com
Ad