Bird
Raised Fist0
Firebasecloud~10 mins

Why serverless functions extend Firebase - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize Firebase Functions in your project.

Firebase
const functions = require('[1]');
Drag options to blanks, or click blank then click option'
Afirebase-functions
Bfirebase-admin
Cfirebase-auth
Dfirebase-storage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'firebase-admin' instead of 'firebase-functions' to write functions.
Confusing storage or auth packages with functions.
2fill in blank
medium

Complete the code to create an HTTP triggered serverless function.

Firebase
exports.helloWorld = functions.[1]((req, res) => { res.send('Hello from Firebase!'); });
Drag options to blanks, or click blank then click option'
Ahttps.onCall
Bhttps
Cdatabase
Dhttps.onRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'https' without '.onRequest' which is incomplete.
Using 'https.onCall' which is for callable functions from clients.
3fill in blank
hard

Fix the error in the function trigger to respond to Firestore document creation.

Firebase
exports.newUser = functions.firestore.document('[1]').onCreate((snap, context) => { console.log('New user added'); });
Drag options to blanks, or click blank then click option'
Ausers/userId
Busers
Cusers/{userId}
Dusers/*
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'users/userId' which listens to a fixed document, not any document.
Using 'users' which is a collection, not a document path.
4fill in blank
hard

Fill both blanks to create a function that triggers on Firebase Authentication user creation and logs the user ID.

Firebase
exports.logNewUser = functions.auth.user().[1]((user) => { console.log('User ID:', user.[2]); });
Drag options to blanks, or click blank then click option'
AonCreate
Buid
ConDelete
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'onDelete' instead of 'onCreate' for user creation events.
Accessing 'email' instead of 'uid' when logging user ID.
5fill in blank
hard

Fill all three blanks to write a Firestore function that updates a user's profile when their document changes.

Firebase
exports.updateUserProfile = functions.firestore.document('[1]').[2]((change, context) => { const newData = change.[3].data(); console.log('Updated data:', newData); });
Drag options to blanks, or click blank then click option'
Ausers/{userId}
BonUpdate
Cafter
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'after' to get new document data.
Using 'onCreate' instead of 'onUpdate' for document changes.

Practice

(1/5)
1. What is the main benefit of using serverless functions with Firebase?
easy
A. They only work on mobile devices.
B. You need to set up and maintain your own servers.
C. They replace the Firebase database completely.
D. You can run backend code without managing servers.

Solution

  1. Step 1: Understand serverless functions

    Serverless functions let you write backend code that runs automatically without you managing servers.
  2. Step 2: Compare options

    Serverless functions do not require server management, do not replace the database, and work on all platforms.
  3. Final Answer:

    You can run backend code without managing servers. -> Option D
  4. Quick Check:

    Serverless = no server management [OK]
Hint: Serverless means no servers to manage [OK]
Common Mistakes:
  • Thinking serverless means no backend code
  • Confusing serverless with database replacement
  • Believing serverless only works on mobile
2. Which of the following is the correct way to define a Firebase serverless function triggered by a database write?
easy
A. exports.myFunc = functions.database.ref('/path').onWrite((change, context) => { /* code */ });
B. functions.database.onWrite('/path', (change, context) => { /* code */ });
C. exports.myFunc = functions.onWrite('/path', (change, context) => { /* code */ });
D. functions.database.ref('/path').write((change, context) => { /* code */ });

Solution

  1. Step 1: Recall Firebase function syntax

    The correct syntax uses exports to define the function and functions.database.ref(path).onWrite() to trigger on database writes.
  2. Step 2: Check each option

    exports.myFunc = functions.database.ref('/path').onWrite((change, context) => { /* code */ }); matches the correct syntax. The other options have incorrect method calls or missing exports.
  3. Final Answer:

    exports.myFunc = functions.database.ref('/path').onWrite((change, context) => { /* code */ }); -> Option A
  4. Quick Check:

    Correct Firebase function syntax = exports.myFunc = functions.database.ref('/path').onWrite((change, context) => { /* code */ }); [OK]
Hint: Look for exports and onWrite with ref path [OK]
Common Mistakes:
  • Omitting exports keyword
  • Using wrong method names like write instead of onWrite
  • Incorrect function chaining order
3. Given this Firebase function code:
exports.greetUser = functions.auth.user().onCreate((user) => {
  return `Hello, ${user.email}!`;
});

What will this function do when a new user signs up?
medium
A. Return a greeting string but does not send it anywhere.
B. Trigger an error because return type is wrong.
C. Send a greeting email automatically.
D. Delete the user account immediately.

Solution

  1. Step 1: Analyze function trigger and return

    The function triggers on user creation and returns a greeting string but does not send or log it anywhere.
  2. Step 2: Understand side effects

    Returning a string alone does not send emails or cause errors. No deletion code is present.
  3. Final Answer:

    Return a greeting string but does not send it anywhere. -> Option A
  4. Quick Check:

    Return alone does not send email [OK]
Hint: Return value alone doesn't trigger actions [OK]
Common Mistakes:
  • Assuming return sends email automatically
  • Thinking function deletes user without code
  • Believing return type causes error
4. You wrote this Firebase function:
exports.logChange = functions.database.ref('/items/{id}').onUpdate((change, context) => {
  console.log('Item updated');
});

But it never logs anything when the database updates. What is the likely problem?
medium
A. onUpdate is not a valid trigger method.
B. The database path '/items/{id}' is invalid syntax.
C. The function is missing a return statement or promise.
D. console.log cannot be used in Firebase functions.

Solution

  1. Step 1: Check function structure

    Firebase functions should return a promise or value to signal completion. Missing return can cause silent failures.
  2. Step 2: Validate other options

    Path syntax is correct, console.log works for debugging, and onUpdate is valid trigger.
  3. Final Answer:

    The function is missing a return statement or promise. -> Option C
  4. Quick Check:

    Return missing causes no execution [OK]
Hint: Always return or await in Firebase functions [OK]
Common Mistakes:
  • Forgetting to return or await async work
  • Assuming console.log doesn't work
  • Misreading trigger method names
5. You want to extend your Firebase app to send a welcome email only when a new user signs up and their email is verified. Which approach best uses serverless functions to achieve this?
hard
A. Use a database trigger on user data write, ignoring email verification status.
B. Use an auth trigger on user creation, check if emailVerified is true, then send email.
C. Send emails directly from the client app after signup.
D. Use a scheduled function to send emails to all users daily.

Solution

  1. Step 1: Identify correct trigger and condition

    Auth triggers on user creation allow checking user properties like emailVerified immediately.
  2. Step 2: Evaluate options for sending email

    Use an auth trigger on user creation, check if emailVerified is true, then send email, which checks emailVerified before sending, ensuring only verified users get emails. The other options do not meet the requirement precisely.
  3. Final Answer:

    Use an auth trigger on user creation, check if emailVerified is true, then send email. -> Option B
  4. Quick Check:

    Auth trigger + emailVerified check = Use an auth trigger on user creation, check if emailVerified is true, then send email. [OK]
Hint: Check emailVerified in auth trigger before emailing [OK]
Common Mistakes:
  • Sending emails without verifying email status
  • Using database triggers for auth events
  • Sending emails from client risking security
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