I have users and jobs collection in my firebase and I want to update the field last_job_posted_date and job_count in the users. I looped every jobs by userId to get the total jobs and the last_job_posted_date. Now my problem is it is not updating. I found out that it executes first the batch.commit() before looping.
async testUpdateUserData() {
this.batch = this.db.firestore.batch();
await this.db.collection('users', ref => ref
.where('role', '==', 'employer').limit(20)).get().subscribe( async usersSnapshot => {
usersSnapshot.forEach(async (userDoc) => {
const userId = userDoc.id;
//fetch data from the "jobs" collection
await this.db.collection('jobs', ref => ref
.where('uid', '==', userId)).get().subscribe( async jobsSnapshot => {
let lastJobPostedDate: Date | null = null;
let jobCount = 0;
jobsSnapshot.forEach(async (jobDoc) => {
// Process each document and update variables
const jobData = jobDoc.data();
if(!lastJobPostedDate || (jobData.created_at > lastJobPostedDate)){
lastJobPostedDate = jobData.created_at;
}
jobCount++;
});
//Prepare the updated data for the user document
const updatedData = {
last_job_posted_date: lastJobPostedDate,
job_count: 0
};
//Create a batched write operation for each user document
const docRef = this.db.collection('users').doc(userId).ref;
await this.batch.update(docRef, updatedData);
});
});
await this.batch.commit().then(() => {
console.log("committed batch write to firestore!")
// reset batch so it can be used again
this.batch = this.db.firestore.batch();
console.log('Batch update completed successfully');
}).catch((error) => {
console.error('Error performing batch update', error);
});
});
}