23

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"
}
Matthew Mullin
  • 7,116
  • 4
  • 21
  • 35
  • According to [this question](https://stackoverflow.com/questions/48040844/how-to-set-server-time-stamp-with-firestore-admin-nodejs-sdk) that should be `admin.firestore.FieldValue.serverTimestamp()`. If that didn't work for you, update your question to show the exact code you tried. – Frank van Puffelen Apr 20 '18 at 13:19

5 Answers5

44

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()
Matthew Mullin
  • 7,116
  • 4
  • 21
  • 35
  • 3
    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. – Doug Stevenson Aug 30 '18 at 16:25
  • 1
    Thanks @DougStevenson. Awesome to see how you're always happy to help out. Even on 4 month old questions – Matthew Mullin Aug 31 '18 at 06:58
4

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

Hamiora
  • 561
  • 5
  • 5
2

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);
});

});

Bishoy Hanna
  • 4,539
  • 1
  • 29
  • 31
2

This worked for me from node:

firebase = require('firebase')
firebase.firestore.Timestamp
Steven Soroka
  • 19,404
  • 4
  • 52
  • 40
1

In Angular, you can access it by

import firebase from 'firebase/app';

createdAt?: firebase.firestore.Timestamp;
Balu
  • 398
  • 6
  • 15