How do I get the server timestamp in Cloud Functions for Firebase with Firestore?

Use server timestamp with Firestore is little different:

// Get the `FieldValue` object
var FieldValue = require("firebase-admin").FieldValue;

// Create a document reference
var docRef = db.collection('objects').doc('some-id');

// Update the timestamp field with the value from the server
var updateTimestamp = docRef.update({
    timestamp: FieldValue.serverTimestamp()
});

if it's not working you can edit the var FieldValue = require("firebase-admin").FieldValue; with var FieldValue = require("firebase-admin").firestore.FieldValue;


Here's my way of doing it. It doesn't require adding '@google-cloud/firestore' as a dependency to your project, and eliminates adding lots of admin.firestore.xxx to your code.

import * as admin from "firebase-admin";

import FieldValue = admin.firestore.FieldValue;
// import anything else you want to alias

someRef.set({timestamp: FieldValue.serverTimestamp()});

Here's a slightly more concise way of doing it:

import * as admin from "firebase-admin"

const timestamp = admin.firestore.FieldValue.serverTimestamp();