我在尝试定期更新文档集合或尝试写入大量数据时遇到 Firestore 批量提交问题。Firestore 似乎只是冻结并且不会发送响应。在这两种情况下,都会遵守最大批量大小 (500) 和文档大小的建议。例如,使用 cron-jobs,提交会成功运行一段时间,然后完全停止,没有任何错误跟踪。
例如,下面的代码在开始出现错误之前会连续执行 6 次。
public updateCoins = async () =>{
const markets = await this.getMarkets('usd')
const maxCount = Math.round(markets.length/this.FB_BATCH_SIZE) + 1
for(let i=0; i< maxCount; i++){
const marketBatch = markets.slice(i*this.FB_BATCH_SIZE, (i+1)*this.FB_BATCH_SIZE > markets.length ? markets.length : (i+1)*this.FB_BATCH_SIZE)
const batchCoins = this.fbService.db.batch()
marketBatch.map((entry)=>{
const coin = this.mapCoin(entry)
if(coin){
const coinRef = this.coinsCol.doc(coin.id);
batchCoins.set(coinRef,coin,{merge:true})
}
})
await batchCoins.commit()
.then(()=>{
console.log('Commited coins batch update')
}).catch((_e)=>{
console.log('Exception saving coins',_e)
})
}
}Run Code Online (Sandbox Code Playgroud)