【发布时间】:2021-05-31 09:31:41
【问题描述】:
我正在尝试从 firestore 获取一堆 (10-1500) 文件,然后一旦我得到它们,我会检查我得到的数量,然后从那里处理它。
只有在抓取文档和检查数组之间有手动“setTimeout”时,我才能让它工作。
在我检查之前,该数组没有被所有的 Firestore 文档填充,但它在一个异步函数中,所以我很困惑为什么它不等待。
- 这不是 Firestore 问题,我按预期收到了所有文件。
javascript 异步函数
async function query(units, days){
try {
let allLogs = []
var ts = moment().subtract(days,'days').valueOf() //ms timestamp from 1 -3 days ago
await units.forEach(async(e) => {
let ref = db.collection('units').doc(`${e.unit.superId}`).collection('logs').where('info.ms', '>=', parseInt(ts))
let docs = await ref.get() // get all subcollection documents from parent document
await docs.forEach(async(doc) =>{ // Loop through and push all documents to allLogs array
allLogs.push(doc.data())
})
})
//If i dont add this: it will always think allLogs is empty. Not waiting long enough for it to be filled
await new Promise(resolve => setTimeout(resolve, (days+ 1.5) * 1000 )); // x seconds wait for array to be filled
if (allLogs.length > 0){
console.log('filled')
} else {
console.log('empty')
}
} catch(err) {
console.log(err)
}
}
【问题讨论】:
标签: javascript arrays google-cloud-firestore async-await