11
votes

Yesterday, all my firebase functions started throwing the following warning:

The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:

const firestore = new Firestore();
const settings = {/* your settings... */ timestampsInSnapshots: true};  
firestore.settings(settings);

With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:

// Old:   const date = snapshot.get('created_at');
// New:   const timestamp = snapshot.get('created_at');   const date =
timestamp.toDate();

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.

Now I want to init my firestore correctly according to this warning. i'm using typescript.

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';    
admin.initializeApp(functions.config().firebase);
let fireStoreDB = fireStoreDB || admin.firestore()

the firestore() that return from admin doesn't have a .settings() method as described in the warning nor it gets an object in the constructor. (It gets an App Object). so I have two questions:

  1. How can init my firestore to get the settings object?

  2. when I insert a date to a document or when I query a document do I also need to pass the Firestore.Timestamp object? or can i query/insert the normal JS Date object and it will get converted automatically?

Thanks.

EDIT:

i managed to solve it for http functions using :

if (!fireStoreDB){
    fireStoreDB = admin.firestore();
    fireStoreDB.settings(settings);
}

But it still is happening on firestore triggers. anyone knows how to give default settings to admin on :

admin.initializeApp(functions.config().firebase);

so it will not happen on firestore triggers?

4
I use JavaScript, not TypeScript and admin.firestore().settings(...) compiles for me. What error do you get?Bob Snyder
Property 'settings' does not exist on type 'Firestore'Shahar Wider
AFAIK Firestore triggers use a separate Firestore client initialized internally by the Functions SDK. This instance uses its own settings, which currently does not include the timestampInSnapshots option.Hiranya Jayathilaka
Thank you Hiranya.... so nothing to do regarding this warning that happends repeatedly currently?Shahar Wider
@ShaharWider, please have a look at my answer as after firebase-admin v7.0.0 there were some breaking changes introduced, regarding this issue. Thanks!Yulian

4 Answers

12
votes

You are still receiving JS Date instead of Firestore Timestamp due to a bug... now fixed in Firebase Functions v2.0.2. See: https://github.com/firebase/firebase-functions/releases/tag/v2.0.2.

For initialisation I've used admin.firestore().settings({timestampsInSnapshots: true}) as specified in warning message, so the warning has disappeared.

When you add a date to a Document, or use as a query parameter, you can use the normal JS Date object.

4
votes

add below 2nd line code in your index.js firebase functions file

admin.initializeApp(functions.config().firebase);  
admin.firestore().settings( { timestampsInSnapshots: true })
0
votes

solved it the following way:

const settings = {timestampsInSnapshots: true}; 
if (!fireStoreDB){
    fireStoreDB = admin.firestore();
    fireStoreDB.settings(settings);
}
0
votes

For firebase-admin version 7.0.0 or above

You should not be getting this error anymore.

In Version 7.0.0 - January 31, 2019 of firebase-admin there were some breaking changes introduced:

BREAKING: The timestampsInSnapshots default has changed to true.

The timestampsInSnapshots setting is now enabled by default so timestamp fields read from a DocumentSnapshot will be returned as Timestamp objects instead of Date. Any code expecting to receive a Date object must be updated.

Note: As stated in the official docs, timestampsInSnapshots is going to be removed in a future release so make sure to remove it altogether.

For older versions of firebase-admin (6.5.1 and below)

This should do the work:

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

const firestore = admin.firestore();

// Add this magical line of code:
firestore.settings({ timestampsInSnapshots: true }); 

Then in your function use the firestore object directly:

firestore.doc(`/mycollection/${id}`).set({ it: 'works' })