Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a batch write in Firebase.
Firebase
const batch = db.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startBatch' or 'beginBatch' which do not exist.
Using 'batch' directly without 'writeBatch()'.
✗ Incorrect
In Firebase, writeBatch() is used to create a batch write object.
2fill in blank
mediumComplete the code to commit the batch write.
Firebase
await batch.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'execute' or 'send' which are not Firebase batch methods.
Forgetting to await the commit.
✗ Incorrect
The commit() method sends all the writes in the batch to the database.
3fill in blank
hardFix the error in the code to add a document write to the batch.
Firebase
batch.set(docRef, [1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data as a string or array instead of an object.
Using assignment syntax inside the data parameter.
✗ Incorrect
The data must be an object with key-value pairs, like { name: 'Alice' }.
4fill in blank
hardFill both blanks to limit batch size and handle errors.
Firebase
if (batchSize [1] MAX_BATCH_SIZE) { await batch.[2](); batch = db.writeBatch(); batchSize = 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' for the size check.
Using 'execute()' which is not a Firebase batch method.
✗ Incorrect
Check if batch size is greater or equal to max, then commit the batch.
5fill in blank
hardFill all three blanks to create a safe batch write loop.
Firebase
for (const item of items) { const docRef = db.collection('users').doc(item.id); batch.[1](docRef, item.data); batchSize [2] 1; if (batchSize [3] MAX_BATCH_SIZE) { await batch.commit(); batch = db.writeBatch(); batchSize = 0; } } await batch.commit();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' instead of 'set' which may fail if document does not exist.
Using '=' instead of '+=' to increment batchSize.
Using '<=' instead of '>=' in the batch size check.
✗ Incorrect
Use set to add writes, increment batchSize with +=, and check if batchSize is greater or equal to max.