13

So, I've been getting this warning recently:

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.

I am trying to implement the suggested correction in the admin SDK in my Cloud Functions code, since most of what I am doing is through there.

I tried using admin.firestore().settings({ timestampsInSnapshots: true }) but got the following warning:

admin.firestore(...).settings is not a function

How do I solve it?

isherwood
  • 58,414
  • 16
  • 114
  • 157
rgoncalv
  • 5,825
  • 6
  • 34
  • 61

4 Answers4

11

I had the same problem. I had to update firebase-functions and firebase-admin.

To upgrade, go to your CLI, then

ProjectDirectory > Functions > npm install firebase-functions@latest firebase-admin@latest --save

Then, at the top, before triggering functions:

const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);
Jeff Padgett
  • 2,380
  • 22
  • 34
  • Use @Marcus Solution Below as this does not fix Error: Firestore has already been initialized. You can only call settings() once, and only before calling any other methods on a Firestore object. – Kevin Jan 05 '21 at 20:45
  • That is a different implementation issue, and doesn't solve the root of the problem in the question. – Jeff Padgett Mar 06 '21 at 18:52
7

To prevent the "Firestore.settings() has already been called" error, change

db.settings(settings);

to

try{ db.settings(settings); }catch(e){}
0

i solved with:

const settings = { timestampsInSnapshots: true };
const db = admin.firestore();
db.settings(settings);
db.collection('any');
  • 1
    This is almost identical to code I posted before, and, as expected, didn't work :/ – rgoncalv Jul 20 '18 at 14:15
  • I had/have same problem. Try putting the code in your function itself. For example, after .onCreate (or whatever trigger). const firestore = admin.firestore(); const settings = {timestampsInSnapshots: true}; firestore.settings(settings); – Jeff Padgett Jul 20 '18 at 20:21
  • I noticed that if I do this, the error is removed for that function. HOWEVER, if the function is called more than once in quick succession, I get this ERROR: Firestore.settings() has already been called. You can only call settings() once, and only before... etc. That's why this is not an answer. Hoping we can figure it out together. – Jeff Padgett Jul 20 '18 at 20:22
0

Try configuring the settings right after initializing the admin SDK that might help.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 19 '22 at 13:15