📖 Scenario: You are building a simple Firebase Cloud Function that handles HTTP requests to manage user profiles. Each request will contain data about a user, and your function will process this data to create or update user profiles in Firestore.
🎯 Goal: Build a Firebase Cloud Function that correctly uses resource and request objects to read user data from the request and prepare a Firestore document resource for storing user profiles.
📋 What You'll Learn
Create a request object that contains user data fields: userId, name, and email.
Create a resource object representing a Firestore document path for the user profile.
Use the request data to build the Firestore document resource path.
Complete the Cloud Function setup to handle the request and prepare the resource.
💡 Why This Matters
🌍 Real World
This project simulates a backend function that processes user profile data sent from a frontend app and prepares the correct Firestore document path to store or update user information.
💼 Career
Understanding how to use request and resource objects in Firebase Cloud Functions is essential for backend developers working with serverless architectures and real-time databases.
Progress0 / 4 steps
1
Create the initial request object with user data
Create a constant called request that is an object with these exact properties and values: userId set to 'user123', name set to 'Alice', and email set to 'alice@example.com'.
Create a resource object for Firestore document path
Create a constant called resource that is an object with a property path set to the string 'users/user123' exactly.
Firebase
Hint
Use const resource = { path: 'users/user123' }
3
Build the Firestore document path dynamically from request data
Modify the resource object so that its path property is built using the userId from the request object. The path should be 'users/' plus the userId value.
Firebase
Hint
Use template literals: path: `users/${request.userId}`
4
Complete the Cloud Function to handle the request and resource
Write a Firebase Cloud Function named handleUserProfile that takes req and res parameters. Inside, create the request object from req.body, build the resource object with the Firestore path using request.userId, and end the response with status 200 and message 'User profile processed'.
Firebase
Hint
Use functions.https.onRequest and access req.body for user data.
Practice
(1/5)
1. What does a request object in Firebase functions mainly contain?
easy
A. Logs of previous function executions
B. Details about the data or file that triggered the function
C. Settings for Firebase project configuration
D. Information about what the user sent and how
Solution
Step 1: Understand the role of request objects
The request object holds details about the user's input and request method.
Step 2: Differentiate from resource objects
Resource objects describe the trigger data or file, not user input.
Final Answer:
Information about what the user sent and how -> Option D
Quick Check:
Request object = user input info [OK]
Hint: Request = user input details, Resource = trigger data [OK]
Common Mistakes:
Confusing resource object with request object
Thinking request holds trigger file info
Assuming request contains project settings
2. Which of the following is the correct way to access the data sent by a user in a Firebase HTTPS function's request object?
easy
A. request.data.body
B. request.body.data
C. request.payload
D. request.trigger.data
Solution
Step 1: Recall request object structure
In Firebase HTTPS functions, user data is inside request.body.
Step 2: Identify correct property for user data
The user data is accessed as request.body.data if data is nested inside body.
Final Answer:
request.body.data -> Option B
Quick Check:
Access user data via request.body.data [OK]
Hint: User data is inside request.body in HTTPS functions [OK]
Common Mistakes:
Using request.data.body which is invalid
Trying to access request.payload which doesn't exist
A. Parameters are reversed; should be (request, response)
B. Missing return statement
C. Incorrect function name
D. No error; code is correct
Solution
Step 1: Check HTTPS function parameters
HTTPS functions receive (request, response) parameters in that order.
Step 2: Identify parameter misuse
The code uses (resource, request) which reverses and misnames parameters.
Final Answer:
Parameters are reversed; should be (request, response) -> Option A
Quick Check:
HTTPS function params = (request, response) [OK]
Hint: HTTPS functions always use (request, response) parameters [OK]
Common Mistakes:
Swapping request and response parameters
Using resource parameter in HTTPS function
Ignoring parameter order importance
5. You want to write a Firebase function triggered by Firestore document changes. Which object should you use to access the changed document's data inside the function?
hard
A. resource.after.data()
B. request.body
C. context.params
D. resource.data()
Solution
Step 1: Understand Firestore trigger parameters
Firestore triggers receive a change object with before and after snapshots.
Step 2: Access the new document data
The updated document data is accessed via change.after.data(), which is the resource object.
Final Answer:
resource.after.data() -> Option A
Quick Check:
Firestore change after data = resource.after.data() [OK]
Hint: Use resource.after.data() for updated Firestore document data [OK]
Common Mistakes:
Using request.body which is for HTTP functions
Using context.params which holds path variables
Using resource.data() which is old snapshot, not updated
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.