firestore-codelab-extended-swift
firestore-codelab-extended-swift copied to clipboard
step 17. Handling denormalized data
Option 2: Change data server-side with Cloud Functions
batch.update() func returns WriteBatch instance itself, not Promise. So await is not needed.
// update changes to restaurant
async function updateRestaurant(db: Firestore, restaurantID: string, name: string) {
const updateRef = db.collection('reviews');
// query a list of reviews of the restaurant
const queryRef = updateRef.where('restaurantID', '==', restaurantID);
const batch = db.batch();
const reviewsSnapshot = await queryRef.get();
for (const doc of reviewsSnapshot.docs) {
await batch.update(doc.ref, {restaurantName: name}); <--- HERE!
};
await batch.commit();
console.log(`name of restaurant updated to ${name}`);
}
Good catch! At some point, I promise we'll revise this codelab, and I'll make sure to incorporate your feedback.