Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admin SDK cannot set settings for Firestore

So, I've been getting this warning recently:

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.

I am trying to implement the suggested correction in the admin SDK in my Cloud Functions code, since most of what I am doing is through there.

I tried using admin.firestore().settings({ timestampsInSnapshots: true }) but got the following warning:

admin.firestore(...).settings is not a function

How do I solve it?

like image 981
rgoncalv Avatar asked Jul 20 '18 11:07

rgoncalv


People also ask

What is firestore Admin SDK?

The Admin SDK is a set of server libraries that lets you interact with Firebase from privileged environments to perform actions like: Read and write Realtime Database data with full admin privileges.

Can I create admin panel with Firebase?

Start by creating a new app on Retool, and give this app a name. We'll call it “Firebase Admin.” Next, create a Firebase resource by clicking "create a new resource" from the Resource field at the bottom panel. Select Firebase from the options to create a Firebase resource.

How do I connect firestore to Python?

To connect to Firestore, Firebase first performs authentication. To get the credentials for authentication, click on project settings, and click on “service accounts“. In the “Service accounts” tab, you can find a code snippet for connecting to Google Firebase. Select python as the language and copy the code snippet.


1 Answers

I had the same problem. I had to update firebase-functions and firebase-admin.

To upgrade, go to your CLI, then

ProjectDirectory > Functions > npm install firebase-functions@latest firebase-admin@latest --save

Then, at the top, before triggering functions:

const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);
like image 89
Jeff Padgett Avatar answered Sep 30 '22 18:09

Jeff Padgett