问题描述
我使用批处理写入firestore
,而我的firebase-functions
代码总是在第一次尝试时成功执行。
i functions: Beginning execution of "myFunction"
i functions: Finished "myFunction" in ~1s
i functions: Beginning execution of "myFunction"
API fetch ...
Amount of records to process: 3600
filled batch (amount of writes: 490)
filled batch (amount of writes: 490)
...
All batches commited to Firebase
imported batch
imported batch
...
i functions: Finished "myFunction" in ~33s
该错误总是在函数的第二次或第三次执行时发生,并显示以下错误消息:
Unhandled error Error: Cannot modify a WriteBatch that has been committed.
- 我创建了一个具有批次的数组,并向每个批次传递了490次写入。
- 我执行
await Promise.all()
内的每个提交操作
我想我的错误是在步骤2中。我尝试了Promise链和异步等待:
异步/等待:
try {
await Promise.all(batches.map(batch => batch.commit()))
console.log('All batches commited to Firebase')
} catch (error) {
console.log(error)
}
承诺链:
await Promise.all(batches.map(batch => {
return batch.commit().then(function () {
console.log("imported batch")
});
}))
.then(console.log('All batches commited to Firebase'))
.catch(err => console.log(err))
batchs数组本身就是这样创建的,我在循环中多次调用以下函数:
function useBatches(collectionRef,action,type,docId,data) {
const docRef = collectionRef.doc(docId);
switch(action) {
case 'delete':
batches[batchIndex].delete(docRef);
break;
case 'update':
batches[batchIndex].update(docRef,data);
break;
case 'set':
batches[batchIndex].set(docRef,data);
break;
}
batchSizeCounter++;
totalWritesTodo--
// create batches
if (batchSizeCounter === BATCH_SIZE) {
console.log(`filled batch (amount of writes: ${batchSizeCounter})`)
batches.push(db.batch());
batchIndex++;
batchSizeCounter = 0;
}
// inform about last batch was filled
if (totalWritesTodo === 0) {
console.log(`filled batch (amount of writes: ${batchSizeCounter})`)
return;
}
}
非常感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)