Bird
Raised Fist0
Firebasecloud~10 mins

Firebase with Angular - Interactive Code Practice

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

Complete the code to initialize Firebase in an Angular app.

Firebase
import { initializeApp } from 'firebase/app';

const firebaseConfig = {
  apiKey: 'your-api-key',
  authDomain: 'your-auth-domain',
  projectId: 'your-project-id',
};

const app = initializeApp([1]);
Drag options to blanks, or click blank then click option'
AfirebaseConfig
BappConfig
CconfigFirebase
DfirebaseSettings
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name for the config object.
Forgetting to pass any argument to initializeApp.
2fill in blank
medium

Complete the code to import Firestore service from Firebase in Angular.

Firebase
import { getFirestore } from '[1]';

const db = getFirestore(app);
Drag options to blanks, or click blank then click option'
Afirebase/firestore/lite
Bfirebase/firestore
Cfirebase/database
Dfirebase/auth
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from 'firebase/database' which is for Realtime Database.
Using 'firebase/auth' which is for authentication.
3fill in blank
hard

Fix the error in the Angular Firestore query to get a collection.

Firebase
import { collection, getDocs } from 'firebase/firestore';

async function fetchUsers() {
  const usersCol = collection(db, [1]);
  const userSnapshot = await getDocs(usersCol);
  return userSnapshot.docs.map(doc => doc.data());
}
Drag options to blanks, or click blank then click option'
AuserCollection
Busers
C'users'
D'userList'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable without quotes when a string literal is needed.
Using incorrect collection names.
4fill in blank
hard

Fill both blanks to add a document to Firestore with Angular.

Firebase
import { collection, addDoc } from 'firebase/firestore';

async function addUser() {
  const usersCol = collection(db, [1]);
  const docRef = await addDoc(usersCol, [2]);
  return docRef.id;
}
Drag options to blanks, or click blank then click option'
A'users'
B{ name: 'Alice', age: 30 }
C'userData'
D{ username: 'alice123' }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong collection names.
Passing incorrect data types instead of an object.
5fill in blank
hard

Fill all three blanks to update a Firestore document in Angular.

Firebase
import { doc, updateDoc } from 'firebase/firestore';

async function updateUser() {
  const userDoc = doc(db, [1], [2]);
  await updateDoc(userDoc, [3]);
}
Drag options to blanks, or click blank then click option'
A'users'
B'user123'
C{ age: 31 }
D'userId'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names instead of string literals for collection or document ID.
Passing non-object types as update data.

Practice

(1/5)
1. What is the main purpose of integrating Firebase with an Angular application?
easy
A. To run Angular code on the Firebase server
B. To replace Angular's core framework features
C. To provide backend services like database and authentication easily
D. To create static HTML pages without JavaScript

Solution

  1. Step 1: Understand Firebase's role

    Firebase offers backend services such as real-time database and user authentication.
  2. Step 2: Understand Angular's role

    Angular is a frontend framework that can use Firebase to add backend features easily.
  3. Final Answer:

    To provide backend services like database and authentication easily -> Option C
  4. Quick Check:

    Firebase adds backend services to Angular apps = D [OK]
Hint: Firebase adds backend features to Angular apps fast [OK]
Common Mistakes:
  • Thinking Firebase replaces Angular framework
  • Believing Firebase runs Angular code on server
  • Confusing Firebase with static site generator
2. Which is the correct way to initialize Firebase in an Angular app's app.module.ts file?
easy
A. const app = firebase.initializeApp(firebaseConfig);
B. import { initializeApp } from 'firebase/app'; const app = initializeApp(firebaseConfig);
C. import firebase from 'firebase'; firebase.init(firebaseConfig);
D. firebaseApp = new Firebase(firebaseConfig);

Solution

  1. Step 1: Check Firebase modular SDK usage

    Firebase v9+ uses modular imports like initializeApp from 'firebase/app'.
  2. Step 2: Verify correct initialization syntax

    The correct syntax is const app = initializeApp(firebaseConfig); after importing.
  3. Final Answer:

    import { initializeApp } from 'firebase/app'; const app = initializeApp(firebaseConfig); -> Option B
  4. Quick Check:

    Use modular import and initializeApp() = A [OK]
Hint: Use modular import and initializeApp() for Firebase v9+ [OK]
Common Mistakes:
  • Using old namespace import like 'firebase.initializeApp'
  • Calling non-existent method 'firebase.init'
  • Trying to create Firebase instance with 'new Firebase()'
3. Given this Angular Firebase code snippet, what will be the output in the console?
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';

const auth = getAuth();
signInWithEmailAndPassword(auth, 'user@example.com', 'wrongpass')
  .then(() => console.log('Login success'))
  .catch(error => console.log(error.code));
medium
A. 'auth/wrong-password'
B. 'Login success'
C. 'auth/user-not-found'
D. SyntaxError

Solution

  1. Step 1: Understand signInWithEmailAndPassword behavior

    This function tries to sign in with given email and password, returns a promise.
  2. Step 2: Analyze given credentials

    The password is 'wrongpass', which is incorrect, so sign-in fails and triggers catch.
  3. Step 3: Check error code logged

    The catch logs error.code, which for wrong password is 'auth/wrong-password'.
  4. Final Answer:

    'auth/wrong-password' -> Option A
  5. Quick Check:

    Wrong password error code = 'auth/wrong-password' [OK]
Hint: Wrong password triggers 'auth/wrong-password' error code [OK]
Common Mistakes:
  • Assuming success message logs on wrong password
  • Confusing 'user-not-found' with wrong password error
  • Expecting syntax error from correct code
4. You wrote this code in your Angular component to fetch Firestore data but get an error:
import { getFirestore, collection, getDocs } from 'firebase/firestore';

const db = getFirestore();
const usersCol = collection(db, 'users');
const userSnapshot = await getDocs(usersCol);
const userList = userSnapshot.docs.map(doc => doc.data());
console.log(userList);

What is the likely cause of the error?
medium
A. Missing async function to use await
B. Firestore collection name 'users' is invalid
C. getDocs does not exist in firebase/firestore
D. doc.data() returns undefined always

Solution

  1. Step 1: Identify usage of await

    The code uses await getDocs(usersCol) which requires an async function context.
  2. Step 2: Check for async function wrapper

    If this code is not inside an async function, it will cause a syntax or runtime error.
  3. Final Answer:

    Missing async function to use await -> Option A
  4. Quick Check:

    await must be inside async function = B [OK]
Hint: Use await only inside async functions [OK]
Common Mistakes:
  • Assuming collection name 'users' is invalid
  • Thinking getDocs is not part of Firestore SDK
  • Believing doc.data() returns undefined always
5. You want to display a real-time list of messages in your Angular app using Firebase Firestore. Which approach correctly sets up a subscription to update the list automatically when data changes?
import { getFirestore, collection } from 'firebase/firestore';
import { collectionData } from 'rxfire/firestore';

const db = getFirestore();
const messagesCol = collection(db, 'messages');

// What should you do next?
hard
A. Use messagesCol.onSnapshot() without import
B. Call getDocs(messagesCol) once and assign to messages
C. Use collectionData(messagesCol).then(data => this.messages = data);
D. Use collectionData(messagesCol).subscribe(data => this.messages = data);

Solution

  1. Step 1: Understand real-time updates with Firestore

    Firestore supports real-time listeners that update data when changes occur.
  2. Step 2: Use RxFire's collectionData for Angular

    RxFire's collectionData returns an observable you can subscribe to for live updates.
  3. Step 3: Correct subscription syntax

    Using collectionData(messagesCol).subscribe(data => this.messages = data); sets up real-time updates properly.
  4. Final Answer:

    Use collectionData(messagesCol).subscribe(data => this.messages = data); -> Option D
  5. Quick Check:

    Subscribe to collectionData observable for real-time updates = C [OK]
Hint: Subscribe to collectionData observable for live Firestore updates [OK]
Common Mistakes:
  • Using getDocs for one-time fetch instead of real-time
  • Calling onSnapshot without proper import or context
  • Using then() on observable instead of subscribe()
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