This quickstart demonstrates using the Firebase SDK for Cloud Functions with Cloud Firestore.
This sample app does two things:
- Creates messages in Cloud Firestore using a simple HTTPS request which is handled by an HTTP function. Writing to Cloud Firestore is done using the Firebase Admin SDK.
- When a message gets added in Cloud Firestore, a function triggers and automatically makes these messages all uppercase.
Before you can test the functions locally or deploy to a Firebase project,
you'll need to run npm install
in the functions
directory.
The Firebase Local Emulator Suite allows you to build and test apps on your local machine instead of deploying to a Firebase project.
-
Create a Firebase project in the Firebase Console
Wondering why this step is needed? Even though the emulator will run this sample on your local machine, it needs to interact with a Firebase project to retrieve some configuration values.
-
Run
firebase emulators:start
-
Open the Emulator Suite UI
- Look in the output of the
firebase emulators:start
command for the URL of the Emulator Suite UI. It defaults to localhost:4000, but may be hosted on a different port on your machine. - Enter that URL in your browser to open the UI.
- Look in the output of the
-
Trigger the functions
- Look in the output of the
firebase emulators:start
command for the URL of the http function "addMessage". It will look similar to:http://localhost:5001/MY_PROJECT/us-central1/addMessage
MY_PROJECT
will be replaced with your project ID- The port may be different on your local machine
- Add the query string
?text=uppercaseme
to the end of the function's URL. It should now look something like:http://localhost:5001/MY_PROJECT/us-central1/addMessage?text=uppercaseme
- Optionally, you can change the message "uppercaseme" to a custom message
- Create a new message by opening the URL in a new tab in your browser
- Look in the output of the
-
View the effects of the functions in the Emulator Suite UI
-
In the "Logs" tab, you should see new logs indicating that the functions "addMessage" and "makeUppercase" ran:
functions: Beginning execution of "addMessage"
functions: Beginning execution of "makeUppercase"
-
In the "Firestore" tab, you should see a document containing your original message as well as the uppercased version of your message (if it was originally "uppercaseme", you'll see "UPPERCASEME")
-
To deploy and test the sample:
- Create a Firebase project on the Firebase Console
- Deploy your project's code using
firebase deploy
- Create a message by opening the following URL in your browser: https://us-central1-[MY_PROJECT].cloudfunctions.net/addMessage?text=uppercaseme (Replace [MY_PROJECT] by your project ID and you can change the message "uppercaseme").
You should see your text value displayed in the console and uppercase.
We'd love that you contribute to the project. Before doing so please read our Contributor guide.
© Google, 2016. Licensed under an Apache-2 license.