All of my other functions are working fine and this function works with a limited data set, so it's not really a credentials problem. It appears that I am running into the issue describes in these answers:
https://stackoverflow.com/a/58828202/3662110
https://stackoverflow.com/a/58779000/3662110
I'm fetching a pretty large chunk of JSON, so I think the issue is similar in that it's timing out, but I'm unable to add return
or await
in the same way described in those answers (tried them both, but return
does nothing and await
causes another error) because I'm looping through the data and writing many documents. This function has worked in the past when I limit the number of results fetched from the API. How do I keep the function alive long enough to write all the data?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports.fetchPlayers = functions.pubsub
.schedule('every Tuesday of sep,oct,nov,dec 6:00')
.timeZone('America/New_York')
.onRun(async context => {
const response = fetch(
`<<< the endpoint >>>`,
{<<< the headers >>>}
)
.then(res => {
<<< handle stuff >>>
})
.then(res => res.json());
const resObj = await response;
const players = [];
resObj.dfsEntries[0].dfsRows.forEach(x => {
<<< extract the data >>>
players.push({ id, fName, lName, pos, team, [week]: fp });
})
players.forEach(player =>
admin
.firestore()
.collection('players')
.doc(`${player.id}`)
.set(player, {merge: true})
.then(writeResult => {
// write is complete here
})
);
});