12

Cloud Functions - Cloud Firestore error: can't get serverTimestamp

const admin = require('firebase-admin');
    exports.userlog = functions.firestore
    .document('user/{userId}')
    .onUpdate((change, context) => 
    { 
        const db = admin.firestore();
        //var timestamp = db.FieldValue.serverTimestamp();
        var timestamp = db.ServerValue.TIMESTAMP;
        ...
        return db.collection('userlog').add(
        {
            userId : previousValue.userId,
            ...
            timestamp: timestamp
        }).then(ref => 
        {
            return console.log('Added document with ID: ', ref.id);
        });
    });

I got two errors separately:

TypeError: Cannot read property 'serverTimestamp' of undefined

TypeError: Cannot read property 'TIMESTAMP' of undefined

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jack Wilson
  • 6,065
  • 12
  • 29
  • 52
  • 1
    Did you figure out how to solve this? I tried the suggested answer you received but it doesn't work. I get the same serverTimestamp error as you – Mel Dec 21 '19 at 04:13

4 Answers4

22

The correct syntax is:

firebase.firestore.FieldValue.serverTimestamp()

Note the lack of parenthesis (()) after firestore: this is a static variable, not an instance variable/member field.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
3
// Imports: Dependencies
const admin = require('firebase-admin');

// Correct Syntax (If You Don't Want To Import Firestore)
created_at: admin.firestore.FieldValue.serverTimestamp(),
jefelewis
  • 1,850
  • 1
  • 27
  • 63
  • 5
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 11 '20 at 10:21
1

since @FrankvanPuffelen's edit queue is full with missing imports, documentation, etc I'll post an update on his solution


include from (web version 8):

const firebase = require('firebase-admin');

or use more recent (web version 9):

import {FieldValue} from "firebase/firestore";

The correct syntax is:

firebase.firestore.FieldValue.serverTimestamp()

Note the lack of parenthesis (()) after firestore: this is a static variable, not an instance variable/member field.

See Firebase example for more info

CybeX
  • 2,060
  • 3
  • 48
  • 115
0

I was using the correct syntax as per the upvoted answer(s), but still go this error. It turns out it had something to do with my version of the firebase CLI being off-sync with versions of the firebase-admin package (I'm not sure how it happened in the first place, but I noticed form my source tracking tool that the package.json and lock files had been updated).

Solved the problem by deleting all changes I had made on my package.json & lock files and re-installed the CLI:

npm i -g firebase-tools

kip2
  • 6,473
  • 4
  • 55
  • 72