我似乎无法弄清楚我哪里出了问题,我尝试将代码放入异步函数中并等待循环完成,但它以某种方式在完成之前不断提交 writebatch。
请有人解释一下我的代码,我不知道我在哪里可能不理解这个过程。
我设法研究的是异步函数在后台运行并允许其他代码运行,因此如果我需要在之后完成工作,我需要先等待异步操作。
请让我知道我缺少什么,感谢帮助,这是我的代码:
getReady();
async function stage1() {
const batch = db.batch();
await response.data.matches.forEach(async match => {
var batchRef = db.collection('matches').doc(`${match.id}`);
var utcDate = `${match.utcDate}`; // ISO-8601 formatted date returned from server
var localDate = moment.utc(utcDate).toDate();
var unixTime = ((localDate.getTime()) / 1000);
const now = new Date();
const secondsSinceEpoch = Math.round(now.getTime() / 1000)
var howLong = timeDifference(unixTime, secondsSinceEpoch);
var checkMatches = db.collection('matches');
matchesSnapshot = await checkMatches.where('matchId', '==',
match.id).get();
if (matchesSnapshot.empty) {
batch.set(batchRef, {
competitionName: `${match.competition.name}`,
competitionId: `${match.competition.id}`,
matchStatus: …
Run Code Online (Sandbox Code Playgroud) javascript node.js firebase google-cloud-platform google-cloud-firestore