无法修改已在具有Firestore的节点J中提交的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: `${match.status}`,
matchDate: `${match.utcDate}`,
matchDay: `${match.matchday}`,
unixDate: unixTime,
matchId: `${match.id}`,

lastUpdated: `${match.lastUpdated}`,
homeTeamScore: match.score.fullTime.homeTeam,
awayTeamScore: match.score.fullTime.awayTeam,
homeWinOdds: `${match.odds.homeWin}`,
drawOdds: `${match.odds.draw}`,
awayWinOdds: `${match.odds.awayWin}`,
matchResults: `${match.score.winner}`,
matchduration: `${match.score.duration}`,
fullTimeHomeTeam: `${match.score.fullTime.homeTeam}`,
fullTimeAwayTeam: `${match.score.fullTime.awayTeam}`,
halfTimeHomeTeam: `${match.score.halfTime.homeTeam}`,
halfTimeAwayTeam: `${match.score.halfTime.awayTeam}`,
extraTimeHomeTeam: `${match.score.extraTime.homeTeam}`,
extraTimeAwayTeam: `${match.score.extraTime.awayTeam}`,
penaltiesHomeTeam: `${match.score.penalties.homeTeam}`,
penaltiesAwayTeam: `${match.score.penalties.awayTeam}`,
homeTeamId: `${match.homeTeam.id}`,
awayTeamId: `${match.awayTeam.id}`,
homeTeamName: `${match.homeTeam.name}`,
awayTeamName: `${match.awayTeam.name}`,
category: 'Football'

});

} else if (!matchesSnapshot.empty) {

var checkingMatches = db.collection('matches').doc(`${match}`);
var doc = await checkingMatches.get();

var oldTime = doc.data().lastUpdated;

var utcDate2 = `${match.lastUpdated}`; // ISO-8601 formatted date returned from server
var utcDate3 = oldTime; //

var localDate2 = moment.utc(utcDate2).toDate();
var localDate3 = moment.utc(utcDate3).toDate();

var unixTime2 = ((localDate2.getTime()) / 1000);
var unixTime3 = ((localDate3.getTime()) / 1000);

if (unixTime2 > unixTime3) {
const reference = db.collection('matches').doc(`${match.id}`);

batch.update(reference, {
matchStatus: `${match.status}`,
matchDate: `${match.utcDate}`,
matchDay: `${match.matchday}`,

lastUpdated: `${match.lastUpdated}`,
homeTeamScore: match.score.fullTime.homeTeam,
awayTeamScore: match.score.fullTime.awayTeam,
homeWinOdds: `${match.odds.homeWin}`,
drawOdds: `${match.odds.draw}`,
awayWinOdds: `${match.odds.awayWin}`,
matchResults: `${match.score.winner}`,
matchduration: `${match.score.duration}`,
fullTimeHomeTeam: `${match.score.fullTime.homeTeam}`,
fullTimeAwayTeam: `${match.score.fullTime.awayTeam}`,
halfTimeHomeTeam: `${match.score.halfTime.homeTeam}`,
halfTimeAwayTeam: `${match.score.halfTime.awayTeam}`,
extraTimeHomeTeam: `${match.score.extraTime.homeTeam}`,
extraTimeAwayTeam: `${match.score.extraTime.awayTeam}`,
penaltiesHomeTeam: `${match.score.penalties.homeTeam}`,
penaltiesAwayTeam: `${match.score.penalties.awayTeam}`,
});
}

}

});
return batch.commit().then(() => {

console.log("im done");
}).catch((err) => {
console.log('Mac! there was an error while doing the job: ', err);
});

}


async function getReady() {

await stage1();

}

我眼前一亮的是你在第4行的.forEach(async match => {.forEach()NOT异步的-它将NOT等待,它将继续执行-这可能是WriteBatch在异步操作尝试写入之前关闭的原因。

至少,您需要使用类似Promise.All(...whatever.map())的东西(如果愿意,可以丢弃结果(来使整个过程异步。

老实说,在那之后我甚至什么都没看——很可能还有其他问题。

最新更新