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:
How can init my firestore to get the settings object?
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?
admin.firestore().settings(...)
compiles for me. What error do you get? – Bob Snyderfirebase-admin
v7.0.0 there were some breaking changes introduced, regarding this issue. Thanks! – Yulian