0
0
Firebasecloud~30 mins

Resource and request objects in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Firebase Resource and Request Objects
📖 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'.
Firebase
Need a hint?

Use const request = { userId: 'user123', name: 'Alice', email: 'alice@example.com' }

2
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
Need a 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
Need a 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
Need a hint?

Use functions.https.onRequest and access req.body for user data.