Bird
Raised Fist0
Firebasecloud~10 mins

Resource and request objects in Firebase - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Resource and request objects
Client sends request
↓
Firebase receives request
↓
Request object created
↓
Resource object accessed
↓
Process request using resource
↓
Send response back to client
This flow shows how Firebase handles a client request by creating a request object, accessing the resource object, processing the request, and sending a response.
Execution Sample
Firebase
exports.myFunction = (req, res) => {
  const data = req.body;
  const resource = req.resource;
  res.send(`Received: ${JSON.stringify(data)}`);
};
A Firebase function receives a request object with data, accesses the resource object, and sends a response back.
Process Table
StepActionRequest Object StateResource Object StateResponse Sent
1Client sends HTTP POST with JSON bodybody: {"name":"Alice"}resource: undefinednone
2Firebase creates request objectbody: {"name":"Alice"}resource: undefinednone
3Function accesses req.body and req.resourcebody: {"name":"Alice"}resource: undefinednone
4Function processes data and prepares responsebody: {"name":"Alice"}resource: undefinednone
5Function sends response with received databody: {"name":"Alice"}resource: undefinedResponse: Received: {"name":"Alice"}
6Request handling completebody: {"name":"Alice"}resource: undefinedResponse sent
💡 Request processed and response sent to client
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
req.bodyundefined{"name":"Alice"}{"name":"Alice"}{"name":"Alice"}{"name":"Alice"}
req.resourceundefinedundefinedundefinedundefinedundefined
resundefinedundefinedundefinedresponse object readyresponse sent
Key Moments - 2 Insights
Why is req.resource undefined in this example?
In this example, the resource object is not set or used, so it remains undefined as shown in execution_table rows 2 to 6.
How does the function access the data sent by the client?
The function accesses client data through req.body, which holds the JSON sent by the client, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of req.body at step 3?
Aundefined
B{"name":"Alice"}
C{}
Dnull
💡 Hint
Check the 'Request Object State' column at step 3 in the execution_table.
At which step is the response sent back to the client?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Response Sent' column in the execution_table to find when the response is sent.
If the client sends an empty body, how would req.body appear in the variable_tracker after step 2?
A{}
Bnull
Cundefined
D[]
💡 Hint
Empty JSON body is parsed as an empty object {}, check how req.body is shown in variable_tracker.
Concept Snapshot
Firebase functions receive a request object (req) and a response object (res).
req.body holds client data sent in the request.
req.resource can hold resource info if set.
Function processes req and sends response via res.send().
Resource object may be undefined if not used.
Full Transcript
When a client sends a request to a Firebase function, Firebase creates a request object containing the data sent by the client, accessible via req.body. The resource object, req.resource, may or may not be set depending on the function's context. The function processes the request using these objects and sends a response back using the response object res. In the example, the client sends JSON data {"name":"Alice"}, which is available in req.body. The function reads this data and sends back a confirmation message. The resource object remains undefined because it is not used in this example. The execution table shows each step from receiving the request to sending the response, and the variable tracker shows how req.body and res change during execution.

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

  1. Step 1: Understand the role of request objects

    The request object holds details about the user's input and request method.
  2. Step 2: Differentiate from resource objects

    Resource objects describe the trigger data or file, not user input.
  3. Final Answer:

    Information about what the user sent and how -> Option D
  4. 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

  1. Step 1: Recall request object structure

    In Firebase HTTPS functions, user data is inside request.body.
  2. Step 2: Identify correct property for user data

    The user data is accessed as request.body.data if data is nested inside body.
  3. Final Answer:

    request.body.data -> Option B
  4. 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
  • Confusing trigger data with request data
3. Given this Firebase function snippet:
exports.myFunction = functions.storage.object().onFinalize((object, context) => {
  console.log(object.name);
  console.log(context.eventId);
});

What does the object parameter represent?
medium
A. The Firebase project configuration
B. The user's HTTP request data
C. The file that triggered the function
D. The function's execution logs

Solution

  1. Step 1: Identify the trigger type

    The function triggers on a storage object finalization, meaning a file event.
  2. Step 2: Understand the object parameter

    The object parameter holds info about the file that triggered the function.
  3. Final Answer:

    The file that triggered the function -> Option C
  4. Quick Check:

    Storage trigger object = file info [OK]
Hint: Storage triggers pass file info as object parameter [OK]
Common Mistakes:
  • Thinking object is user request data
  • Confusing context with resource object
  • Assuming object holds config or logs
4. Identify the error in this Firebase HTTPS function code:
exports.func = functions.https.onRequest((resource, request) => {
  console.log(request.body);
});
medium
A. Parameters are reversed; should be (request, response)
B. Missing return statement
C. Incorrect function name
D. No error; code is correct

Solution

  1. Step 1: Check HTTPS function parameters

    HTTPS functions receive (request, response) parameters in that order.
  2. Step 2: Identify parameter misuse

    The code uses (resource, request) which reverses and misnames parameters.
  3. Final Answer:

    Parameters are reversed; should be (request, response) -> Option A
  4. 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

  1. Step 1: Understand Firestore trigger parameters

    Firestore triggers receive a change object with before and after snapshots.
  2. Step 2: Access the new document data

    The updated document data is accessed via change.after.data(), which is the resource object.
  3. Final Answer:

    resource.after.data() -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard