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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: