Watch for changes and trigger functions with the Firestore emulator

Jorge Vergara
InstructorJorge Vergara
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 2 years ago

Learn how to listen for changes in your Firestore database and trigger cloud functions.

Instructor: Firebase Cloud Functions can also help you create functions that are triggered by functionality. For example, we have functions that can trigger whenever something changes in our database. In this case, we have an example with Firestore. We're telling the functions that in this path, we're going to have a function and a function ID.

This function ID is dynamic. If we have function/1, function/2, and so on, it's all going to trigger this function. The onCreate says that it's only going to triggered when the new document is created.

Inside the function, what we're doing is that we're getting the ID from the snap. The snap is the snapshot of the data that changed. We're getting whatever is in the body of the document from snap.data. Then, the function, the only thing that's going to do is to log that ID and log the name of the function to the console.log.

To make sure we can use this, the first thing we need to do is navigate into our functions directory and run, npm run build. This way, we're able to build this function before being able to use it in the emulator.

Once that's done, we can go back one folder and type, Firebase emulators start. Once the emulator is up and running, we are going to go into the Firestore emulator, and we are going to create a new collection called functions. This is the one that we said here in the function. It could have been any other name.

I'm going to create a document. This document is just going to have one property, called name. The name is going to be ad message. I click Save, and you see that the document is here now in the database.

If we go into Overview and we open up the functions emulator, you can see that it triggered our Cloud Function. It says, "Beginning execution of...New function created." That's the name of our function here. It's logging the function ID and the name. You can see that the function ID is OORR-something. We can double check that in Firestore, OORR.

If we go back to the logs, you see that it also logged the name that we gave it, the ad message. As you can see, we didn't really have to do any type of configuration to be able to use the emulator for the Firestore trigger functions.

The only thing that you need to keep in mind is that if you are running in the emulator and both the database emulator and the function simulator up and running, any change that you make in the local database is going to trigger the function simulator.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today