Firebase Cloud Functions - Change Firestore settings
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' })
UPDATE as of March, 2019:
If using firebase-admin
version 7.0.0 or above, you no longer need this fix.
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 aDocumentSnapshot
will be returned asTimestamp
objects instead ofDate
. Any code expecting to receive aDate
object must be updated.
What's more, as stated in the official docs, timestampsInSnapshots
is going to be removed in a future release so make sure to remove it altogether.
The advice you're getting in the logs is intended for people using the Firestore node SDK directly. However, when you write Firestore triggers through Cloud Functions, the Admin SDK is initialized automatically, which in turn initializes the Firestore SDK automatically. So, you don't have an opportunity to initialize it yourself.
Until the Firestore SDK is fully finalized, all you can do is make sure that your usage of dates is consistent with the future, fully released Firestore SDK. This means you should use Timestamp objects when reading dates out of snapshots. If you're doing that, you can ignore this warning message.