Firestore moving to timestamps in Firebase functions

筅森魡賤 提交于 2019-12-18 12:27:13

问题


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



回答4:


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' })


来源:https://stackoverflow.com/questions/51418028/firestore-moving-to-timestamps-in-firebase-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!