Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timestampsInSnapshots setting with firebase-admin for NodeJs?

Because of the warning about the upcoming change in timestamp behavior in Firestore, I am trying to change the initialisation code for my app.

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 = firebase.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.

I can't find any reference to firestore "settings" though. Would something like this work? The Typescript definition says no...

import * as admin from "firebase-admin";

export const adminApp = admin.initializeApp();

const firestore = admin.firestore(adminApp);
firestore.settings({ timestampsInSnapshots: true });
like image 588
Thijs Koerselman Avatar asked Jun 11 '18 13:06

Thijs Koerselman


1 Answers

I've upgraded the firebase-admin to 5.13.0 and with the following node.js code in my index.js after initializeApp(), that warning went away and my code runs correctly afterwards.

admin.initializeApp();
const settings = {/* your settings... */ timestampsInSnapshots: true};
admin.firestore().settings(settings);
like image 103
T. Eric Hong Avatar answered Oct 16 '22 05:10

T. Eric Hong