i have this code, i try to create new field in my collection with bacth, i want to verify with this condition ((index + 1) % 500 === 0) to use the commit, what is the wrong?
const myFunction = async () => {
try {
let batch = db.batch()
const batchCommits = []
await schoolList.forEach(async (school, index) => {
await ref
.doc(school.id)
.collection('mycollection')
.where('visual', '==', 'none')
.get()
.then(async (querySnapshot) => {
if (querySnapshot.empty) {
const curses = await ref
.doc(school.id)
.collection('curses')
.doc()
batch.set(curses, common)
if ((index + 1) % 500 === 0) {
batchCommits.push(batch.commit())
batch = db.batch()
}
}
})
})
batchCommits.push(batch.commit())
return Promise.all(batchCommits)
}
}
i am getting this error: Error: Cannot modify a WriteBatch that has been committed.