Currently, I am working on setting up a cron job to monitor the completion of my tournaments and trigger some specific code upon completion. For reference, I came across this example:
During deployment of my code, an error popped up as follows:
ERROR: functions/src/index.ts:23:9 - Expression has type `void`. Put it on its own line as a statement.
Below is the taskRunner function that I've been using:
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);
});
The completeTournament() function can be found further in the file:
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);
});
}
I'm relatively new to Typescript and suspect that I might be misusing function pointers. Any assistance on this matter would be highly appreciated. Thank you in advance.