我正在尝试设置一个cron作业,以查找我的锦标赛何时完成并运行一些完成代码。我举了一个例子:
https://fireship.io/lessons/cloud-functions-scheduled-time-trigger/
当我尝试部署我的代码时,我会得到以下错误:
ERROR: functions/src/index.ts:23:9 - Expression has type `void`. Put it on its own line as a statement.
这是我的taskRunner函数:
export const taskRunner = functions.runWith( { memory: '2GB' })
.pubsub
.schedule('* * * * *').onRun(async context => {
// Consistent timestamp
const now = admin.firestore.Timestamp.now();
// Query all documents ready to perform
const query = db.collection('tournaments').where('endDate', '<=', now).where('winnerUserId', '==', null);
const tournaments = await query.get();
// Tasks to execute concurrently.
const tasks: Promise<any>[] = [];
// Loop over documents and push task.
tournaments.forEach(snapshot => { // <-- error occurs on this line
const { tournamentId, name } = snapshot.data();
const task = completeTournament(tournamentId)
.then(() => console.log("cron job", "Tournament '" + name + "' (id: " + tournamentId + ") completed successfully."));
.catch((err) => console.log("cron job", "Tournament '" + name + "' (id: " + tournamentId + ") encountered an error: " + err));
tasks.push(task);
});
// Execute all jobs concurrently
return await Promise.all(tasks);
});
completeTournament((函数在以下文件中进一步定义:
function completeTournament(tournamentId: string) {
// Get the top entry user id
db.collection("tournaments").doc(tournamentId).get()
.then(tournamentDoc => {
const winnerUserId = tournamentDoc.get("rank[0].userId")
db.collection("tournaments")
.doc(tournamentId)
.update({ "winnerUserId": winnerUserId })
.catch(err => {
console.log("Error completing tournament '" + tournamentId, err);
});
})
.catch(err => {
console.log("Error retrieving tournament '" + tournamentId, err);
});
}
我是Typescript的新手,所以我可能做的函数指针不正确。如有任何帮助,我们将不胜感激。提前谢谢。
对于任何在未来寻找答案的人:
你可以从我原来帖子上的评论中看到,我将问题缩小到了taskRunner
中completeTournament()
上的.then().catch()
调用。我最终没有在那里使用这些调用,而是在实际的工作函数中使用.then().catch()
。