0
0
Firebasecloud~10 mins

Transaction basics in Firebase - Interactive Code Practice

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

Complete the code to start a transaction in Firebase.

Firebase
db.runTransaction([1]);
Drag options to blanks, or click blank then click option'
AupdateData
BtransactionFunction
CcommitTransaction
DstartTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a function.
Using method names that do not exist on the database object.
2fill in blank
medium

Complete the code to get a document reference inside a transaction.

Firebase
const docRef = db.collection('users').doc([1]);
Drag options to blanks, or click blank then click option'
ArefId
Btransaction
CdocData
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using the transaction object instead of the document ID.
Passing undefined or wrong variable names.
3fill in blank
hard

Fix the error in reading data inside the transaction function.

Firebase
return transaction.get(docRef).then(doc => {
  if (!doc.exists) {
    throw new Error('Document does not exist');
  }
  const newCount = doc.data().count [1] 1;
  transaction.update(docRef, { count: newCount });
});
Drag options to blanks, or click blank then click option'
A+
B+=
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using += inside an expression causes syntax errors.
Using subtraction or multiplication instead of addition.
4fill in blank
hard

Fill both blanks to correctly update a field and commit the transaction.

Firebase
transaction.[1](docRef, { [2]: newValue });
Drag options to blanks, or click blank then click option'
Aupdate
Bset
Ccount
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using set which replaces the whole document.
Using incorrect field names.
5fill in blank
hard

Fill all three blanks to complete the transaction function that increments a user's score.

Firebase
db.runTransaction(async (transaction) => {
  const docRef = db.collection('users').doc([1]);
  const doc = await transaction.get(docRef);
  if (!doc.exists) {
    throw new Error('User not found');
  }
  const newScore = doc.data().[2] [3] 1;
  transaction.update(docRef, { score: newScore });
});
Drag options to blanks, or click blank then click option'
AuserId
Bscore
C+
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for document ID or field.
Using incorrect operators like - or *.