[英]Error: could not load the default credentials — creds are fine, function dies
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.我所有的其他功能都工作正常,这个 function 可以使用有限的数据集,所以这不是一个真正的凭据问题。 It appears that I am running into the issue describes in these answers:
看来我遇到了这些答案中描述的问题:
https://stackoverflow.com/a/58828202/3662110 https://stackoverflow.com/a/58828202/3662110
https://stackoverflow.com/a/58779000/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.我正在获取相当大的 JSON 块,所以我认为问题是相似的,因为它超时了,但我无法以这些答案中描述的相同方式添加
return
或await
(尝试了它们,但return
确实什么都没有, await
会导致另一个错误),因为我正在遍历数据并编写许多文档。 This function has worked in the past when I limit the number of results fetched from the API.当我限制从 API 获取的结果数量时,这个 function 过去一直有效。 How do I keep the function alive long enough to write all the data?
如何让 function 存活足够长的时间来写入所有数据?
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
})
);
});
In order to correctly terminate a Cloud Function , you are required to return a promise from the function that resolves when all of the asynchronous work is complete.为了 正确终止 Cloud Function ,您需要从 function 返回一个 promise ,当所有异步工作完成后即可解决。 You can't ignore any promises.
你不能忽视任何承诺。 Calling
then
on a promise isn't enough, since then
just returns another promise. then
在 promise 上调用是不够的,因为then
只返回另一个 promise。 The problem is that you're ignoring the promises generated inside the forEach loop.问题是您忽略了在 forEach 循环中生成的承诺。 You will need to collect all those promises in an array, then use Promise.all() to create a new promise that you can return from the function.
您需要将所有这些承诺收集到一个数组中,然后使用 Promise.all() 创建一个新的 promise,您可以从 function 返回它。 To put it briefly:
简单地说:
const promises = players.map(player => {
return admin.firestore....set(...)
})
return Promise.all(promises)
If you don't wait for all the promises to resolve, then the function will terminate and shut down early, before the async work is done.如果您不等待所有承诺解决,那么 function 将在异步工作完成之前提前终止并关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.