Firestore moving to timestamps in Firebase functio

2019-02-10 08:18发布

问题:

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?

回答1:

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.



回答2:

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

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


回答3:

solved it the following way:

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