How to access firestore.Timestamp from Firebase Cloud Function

firebase firestore timestamp
firestore timestamp to date javascript
firebase-functions timestamp
firestore server timestamp
firestore timestamp to date android
firebase timestamp android
flutter firestore timestamp
firebase timestamp to date (swift)

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"
}

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()

firebase.firestore.Timestamp, 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  Using the following in the cloud function which is equivalent of Timestamp.now() on client side, this returns the current Timestamp. admin.firestore.Timestamp.now() But if you want to initialise Timestamp from Date object you can do it as follows. admin.firestore.Timestamp.fromDate(new Date())

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.

Compare two Firestore Timestamps in Cloud Functions, Is there a way to do this with firebase cloud functions? The code below shows how I get the latest data and edit it! I want to use the latest data's etotal value to  The sample Cloud Function is triggered by a web request, which you can simulate with curl. Prerequisites. Create a project in the Google Cloud Platform Console. Enable billing for your project. Enable the Google Cloud Functions API. Enable the Google Cloud Firestore API (with Cloud Firestore in Native mode).

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

FR: Support Firestore Timestamp · Issue #292 · firebase/firebase , We share code with backend and frontend, so we need to be able to How does one access the Timestamp type? admin.firestore(). validateInteger (/Users/app/​functions/node_modules/@google-cloud/firestore/build/src/  Note: Reads and writes performed in Cloud Functions are not controlled by your security rules, they can access any part of your database. Limitations and guarantees. Cloud Firestore triggers for Cloud Functions is a beta feature with some known limitations: It can take up to 10 seconds for a function to respond to changes in Cloud Firestore.

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

});

Date not working after upgrade? Firestore timestamp : Firebase, I didn't get that the object returned from Firestore is of type TImestamp and have a of Firebase as really snappy but when I went through the Cloud Firestore Web As an experiment, I changed that code to not use transactions, and the time  This quickstart shows you how to set up Cloud Firestore, add data, then view the data you just added in the Firebase console. If you haven't already, create a Firebase project: In the Firebase console, click Add project , then follow the on-screen instructions to create a Firebase project or to add Firebase services to an existing GCP project.

This worked for me from node:

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

The secrets of Firestore's FieldValue.serverTimestamp , If you've written any code that deals with timestamps in Firestore, you've almost certainly encountered the notion of “server timestamps” using  Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network

Adding data | Firestore, To create or overwrite a single document, use the set() method: Timestamp. IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do  There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data. Set a listener to receive data-change events. When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and

How to access firestore.Timestamp from Firebase Cloud Function, 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  Cloud Functions can handle events in Cloud Firestore with no need to update client code. You can read and/or update Cloud Firestore in response to these events using the Firestore APIs and client libraries. In a typical lifecycle, a Cloud Firestore function does the following: Waits for changes to a particular document.

Firestore Order by Document ID or Field, Create Data const timestamp = firebase.firestore. Another option is to use a Firestore Cloud Function to update a count for each runTransaction(async transaction => { // Get the metadata document and increment the  Cloud Functions for Firebase are Google’s serverless solution for Firebase apps. They can be used as the (R)eactor functions for FIRE Stack app architecture. If you’ve developed with firebase-queue, AWS Lambda or some other Functions-as-a-Service architecture, Cloud Functions for Firebase should feel natural… just a lot slicker and easier

Comments
  • 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.
  • 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.
  • Thanks @DougStevenson. Awesome to see how you're always happy to help out. Even on 4 month old questions
  • 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.
  • Sorry, I'm not really in touch with how the authors of that SDK do their releases.
  • 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 :)
  • Big fail on Google's side here... I just opened a bug report with them.