1
votes

I have a simple node script I run to update data in a Firestore database. I used it a couple of hours ago, worked fine. Got dinner, came back, and now I get this error when I run it:

node ./json-to-firestore.js 

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

The example provided in the error does not apply to my case. I've looked for help on this issue, but all the posts seem to be angular-specific. This is all I'm trying to do:

var admin = require("firebase-admin");

var serviceAccount = require("./service-key.json");

const data = require("./occ-firestore.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "xxxxxxxxxxx"
});

data && Object.keys(data).forEach(key => {
    const nestedContent = data[key];

    if (typeof nestedContent === "object") {
        Object.keys(nestedContent).forEach(docTitle => {
            admin.firestore()
                .collection(key)
                .doc(docTitle)
                .set(nestedContent[docTitle])
                .then((res) => {
                    console.log("Document successfully written!");
                })
                .catch((error) => {
                    console.error("Error writing document: ", error);
                });
        });
    }
});

I execute this script by running:

node ./json-to-firestore.js

I'm running NodeJS 8.11.3.

I checked Google's docs and there's no reference to this new behavior.

Can someone provide me with a suggestion? Thanks in advance!

3
This change has been around for a quite some time. Perhaps you updated your client library recently? Anyway, the suggestion is in the error message - it's quite clear.Doug Stevenson
I think this error started being thrown in firebase-admin 5.13.0Tom
I got this too !DrNutsu
@DougStevenson Thank you for your comment. I agree the message is clear, but I'm having trouble translating their recommendation for use in my code.mrbranden
That's the thing... it's erroring out and not executing. My code has nothing to do with dates, so it's unclear to me why it's failing. If it was just an innocuous warning, I wouldn't be concerned.mrbranden

3 Answers

5
votes

This can be fixed as such:

const admin = require('firebase-admin');
const functions = require('firebase-functions');

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

const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
1
votes

I solve this by downgrade version firebase to this

{
  firebase-admin: "5.12.0",
  firebase-functions: "1.0.1"
}
0
votes

Firebase is changing how Date objects are stored in Firestore. Bellow is an actual warning from them (at the date of posting this answer) whenever you try to save Date objects to Firestore:

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

And more importantly, how to handle these changes so your code does not break:

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