简体   繁体   English

Firebase 函数访问 Firestore 错误:无法加载默认凭据

[英]Firebase Functions Accessing Firestore error: Could not load the default credentials

I've been attempting to get to the bottom of issues with a Firebase function I'm using to update some aggregate data in Firestore.我一直试图深入了解我用来更新 Firestore 中一些聚合数据的 Firebase 函数的问题。 I set up a simple test bed and found that any attempt to access the data triggers the error:我设置了一个简单的测试台,发现任何访问​​数据的尝试都会触发错误:

>  Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (/Users/michael/Documents/htdocs/vue/mjf20/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)

I've attempted to just copy the entire Firebase config into the initializeApp() function, but it still generates the same error.我试图将整个 Firebase 配置复制到 initializeApp() 函数中,但它仍然会生成相同的错误。 Here's the entire test code:整个测试代码如下:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();
const db = admin.firestore();

exports.postCache = functions.https.onRequest((request, response) => {
    console.log("Starting test function");
    db.collection('myCollection').doc('myDoc').get()
    .then(snap => {
        if (!snap.exists) {
            console.log('Document not found');
        } else {
            console.log(snap.data());
            response.send(snap.data());
        }
    })
    .catch(error => {
        console.log('In catch block');
        console.log(error);
        response.send(error);
    });
});

If I take out the db stuff, the function will work fine.如果我取出数据库的东西,该功能将正常工作。 As soon as I add even the simplest firestore request, it generates the error.即使我添加了最简单的 firestore 请求,它也会生成错误。 I've seen this issue asked about before, but the situations seem different, and none of the solutions seems to work.我之前看过这个问题,但情况似乎不同,似乎没有一个解决方案有效。 I'm stumped.我难住了。

The code is perfectly fine.代码完全没问题。

I have implemented it exactly on my testing project using this tutorial .我已经使用本教程在我的测试项目中完全实现了它。

I have added your code to index.js and in my firestore I have added collection myCollection with myDoc with just one testing field and deployed using firebase deploy .我已将您的代码添加到index.js并在我的myDoc添加了带有myDoc集合myCollection ,其中只有一个测试字段,并使用firebase deploy

Everything works fine.一切正常。 You should focus on your environment and deployment steps to figure out what is the problem.您应该关注您的环境和部署步骤以找出问题所在。 This link that you have in the error is mentioning environment variables.您在错误中的此链接提到了环境变量。 Maybe a case as well.也许也是一个案例。

I hope it will help!我希望它会有所帮助!

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法加载默认凭据(Firebase function 到 firestore) - Error: Could not load the default credentials (Firebase function to firestore) Firebase 函数:无法加载默认凭据 - Firebase Functions: Could not load default credentials Firebase 云函数:获取应用程序默认凭据时出现意外错误。 无法加载默认凭据 - Firebase cloud functions: Unexpected error while acquiring application default credentials. Could not load the default credentials Ruby的Firestore-无法加载默认凭据 - Firestore from Ruby - Could not load the default credentials 错误:无法加载默认凭据。 - 云功能 - Error: Could not load the default credentials. - Cloud Functions 错误:无法加载默认凭据。 上下文:firebase 登录:ci 和 firebase 身份验证:导出 - Error: Could not load the default credentials. context: firebase login:ci AND firebase auth:export TwiML 应用程序和 Firebase 抛出未处理的承诺拒绝:错误:无法加载默认凭据 - TwiML app and Firebase throwing Unhandled promise rejection: Error: Could not load the default credentials admin.auth().verifyIdToken(idToken) 错误:在 8.0.0 之后无法使用 firebase-admin 加载默认凭据 - admin.auth().verifyIdToken(idToken) Error: Could not load the default credentials whith firebase-admin after to 8.0.0 firestore函数错误:获取应用程序默认凭据时发生意外错误 - firestore functions Error: Unexpected error while acquiring application default credentials 尝试通过 Next.js API 路由上的 admin.firestore.v1.FirestoreAdminClient 导出备份。 错误:无法加载默认凭据 - Trying to export backup via admin.firestore.v1.FirestoreAdminClient on a Next.js API route. Error: Could not load the default credentials
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM