简体   繁体   English

错误:无法加载默认凭据 — 凭据很好,function 死了

[英]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 块,所以我认为问题是相似的,因为它超时了,但我无法以这些答案中描述的相同方式添加returnawait (尝试了它们,但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.

相关问题 Firebase“无法加载默认凭据。” 仅随机发生 50% 的时间它工作正常,另外 50% 它给了我这个错误 - Firebase "Could not load the default credentials." Only happens randomly 50% of the time it works fine, the other 50% it gives me this error 谷歌云抛出错误:无法在 Nodejs 中加载默认凭据 - Google Cloud Throwing Error : Could not load the default credentials in Nodejs TwiML 应用程序和 Firebase 抛出未处理的承诺拒绝:错误:无法加载默认凭据 - TwiML app and Firebase throwing Unhandled promise rejection: Error: Could not load the default credentials 无法加载默认凭据? (Node.js 谷歌计算引擎教程) - Could not load the default credentials? (Node.js Google Compute Engine tutorial) 为什么最新的 Firebase 服务不能再加载默认凭据 - Why can't latest firebase serve load the default credentials anymore 错误:无法在dojo中加载模块 - Error: Could not load module in dojo firestore函数错误:获取应用程序默认凭据时发生意外错误 - firestore functions Error: Unexpected error while acquiring application default credentials Javascript:脚本死于函数调用 - Javascript: Script dies on Function Call 面对错误(0,(_ wordwrap || _load_wordwrap(...))。默认)(...)(...)。strimStart不是expo init上的函数 - Facing error (0 , (_wordwrap || _load_wordwrap(…)).default)(…)(…).trimStart is not a function on expo init jQuery函数不会在第一页加载时触发,但会在刷新时触发 - jQuery Function not triggering on first page load, but triggering fine on refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM