Firestore moving to timestamps in Firebase functions

ぐ巨炮叔叔 提交于 2019-11-30 07:07:56

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.

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

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

solved it the following way:

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

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