How to access firestore.Timestamp from Firebase Cloud Function

15,772

Solution 1

With the release of V2.0 of Firebase Functions is looks like they've added Timestamp support in the Firebase Admin SDK package.

Have a look at the official docs here.

import { firestore } from 'firebase-admin';
...
const now = firestore.Timestamp.now()

Solution 2

At this very moment in time, Timestamp is simply not available in the latest version of the @google-cloud/firestore npm module for node. You can also see that it's not included in the API docs. Perhaps it will be added in the next release of @google-cloud/firestore.

Solution 3

I came across the same problem and so created a basic node module for Firebase Firestore Timestamps here https://www.npmjs.com/package/firebase-firestore-timestamp

Basic code port from https://www.gstatic.com/firebasejs/4.13.0/firebase-firestore.js

Solution 4

This worked for me from node:

firebase = require('firebase')
firebase.firestore.Timestamp

Solution 5

Since you need it inside a cloud function, then you are working with triggers (onCreate, onUpdate...etc) which carries the timestamp via snap parameter. you can use it as below. (it worked with me)

exports.newUserCreated = functions.firestore.document('users/{userId}').onCreate(async (snap, context) => {
firestore.collection(`users/${userID}/lists`).add({
    'created_time': snap.updateTime,
    'name':'new list name',
}).then(documentReference => {
    console.log("list created");
    return null;
  }).catch(error => {
    console.error('Error creating list', error);
    process.exit(1);
});

});

Share:
15,772

Related videos on Youtube

Matthew Mullin
Author by

Matthew Mullin

Engineer specialising in Data Analytics. Fullstack Web and Android developer.

Updated on June 19, 2022

Comments

  • Matthew Mullin
    Matthew Mullin almost 2 years

    We are in the middle of converting our Firestore Date object to the new Timestamp Objects

    We have done so successfully on the front end by importing firestore

    import { firestore } from 'firebase';
    

    and then replacing all Date object types with firestore.Timestamp

      startDate: firestore.Timestamp;
    

    The problem is I can't seem to find a way to get access to Timestamp in node.

    I have tried logging both the admin and functions object but cant seem to find Timestamp at all

    import * as functions from 'firebase-functions';
    import * as admin from 'firebase-admin';
    ...
    console.log(functions)
    console.log(admin)
    

    These are what I've tried and they have all returned with 'Timestamp does not exist on undefined'

    import * as firebase from 'firebase';
    ...
    firebase.firestore.Timestamp.now()
    

    const firebase = require('firebase')   
    ...
    firebase.firestore.Timestamp.now()
    

    import * as admin from 'firebase-admin';
    ...
    admin.firestore.Timestamp.now()
    

    Here are my package.json dependancies

    "dependencies": {
        "@sendgrid/mail": "^6.2.1",
        "@types/node-fetch": "^1.6.8",
        "chai": "^4.1.2",
        "chai-as-promised": "^7.1.1",
        "cors": "^2.8.4",
        "encodeurl": "^1.0.2",
        "fetch": "^1.1.0",
        "firebase": "^4.13.0",
        "firebase-admin": "^5.12.0",
        "firebase-functions": "^1.0.1",
        "generator-karma": "^2.0.0",
        "google-distance": "^1.0.1",
        "mailgun-js": "^0.13.1",
        "moment": "^2.22.1",
        "node-fetch": "^2.1.2",
        "request": "^2.85.0",
        "sinon": "^4.0.1",
        "typescript": "^2.8.3"
    },
    "private": true,
    "devDependencies": {
        "@angular/cli": "^1.7.4",
        "@types/cors": "^2.8.3",
        "@types/jasmine": "^2.6.6",
        "ts-loader": "^3.5.0",
        "webpack-node-externals": "^1.7.2"
    }
    
    • Frank van Puffelen
      Frank van Puffelen about 6 years
      According to this question that should be admin.firestore.FieldValue.serverTimestamp(). If that didn't work for you, update your question to show the exact code you tried.
  • Matthew Mullin
    Matthew Mullin about 6 years
    Thanks @Doug Stevenson. Any idea when this may be? Id expect if FB is logging a big red message in my console saying "Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will change to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.", that cloud/firestore would be wanting these changes implemented as well.
  • Doug Stevenson
    Doug Stevenson about 6 years
    Sorry, I'm not really in touch with how the authors of that SDK do their releases.
  • DauleDK
    DauleDK almost 6 years
    It's crazy that the firebase client team, did not communicate with the admin team. I have the exact same problem as @MatthewMullin. Big red console warnings, and then you convert everything just to find out that the admin SDK still uses Date, so all code sharing is gone. Looking forward to the next release :)
  • Georg
    Georg almost 6 years
    Big fail on Google's side here... I just opened a bug report with them.
  • Doug Stevenson
    Doug Stevenson over 5 years
    That's not really the "firebase functions" npm package. This is the Firebase Admin SDK, which you can certainly use within Cloud Functions for Firebase, or any node process.
  • Matthew Mullin
    Matthew Mullin over 5 years
    Thanks @DougStevenson. Awesome to see how you're always happy to help out. Even on 4 month old questions