[英]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.