Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 275
Jack Wilson Avatar asked May 16 '18 03:05

Jack Wilson


2 Answers

// Imports: Dependencies
const admin = require('firebase-admin');

// Correct Syntax (If You Don't Want To Import Firestore)
created_at: admin.firestore.FieldValue.serverTimestamp(),
like image 99
jefelewis Avatar answered Sep 28 '22 04:09

jefelewis


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.

like image 21
Frank van Puffelen Avatar answered Sep 28 '22 04:09

Frank van Puffelen