Example Functions accessing Cloud Firestore

Let me show you and briefly explain how to read / write / update documents in Cloud Firestore from a Function.

Photo by Marc-Olivier Jodoin on Unsplash

I wanted to write a function to read some data and then update some other data. The docs aren’t clear on how to do it from a function and I struggled to get it working just right.

Below are some examples of read (get), add and update. Delete is more of the same. Also included is an example of how to check the onWrite method to determine what happened to a document.

The key takeaways:

  • Lines 1 & 2 - require firebase-admin and initializeApp
  • When you use class from the require statement (in my case, admin), use admin.firestore() and then the syntax from there is the same as in my front end code